The usefulness of bool * in C #

Can I use it in bool*any meaningful way. How to convert bool*to byte, for example, or save bool*in byte

My goal is to manage my own memory in my project, features are not important, just something like how to do it. Now I would like to be able to store my own variables, and I need to store a boolean. How can I, using unsafeand byte*, keep this logical value and use my space the best? Ideally store 4 bits per byte.

+3
source share
1 answer

In C #, you usually don’t use one bool*, which you can only use in unsafe code (which generates a lot of other things like pinning objects, etc.). A bool*will be a pointer to a boolean value. The pointer is 4 bytes long and cannot be converted to lossless bytes.

Why do you need it and where do you come across? Typically, there is no easy way to use pointers in C # unless you have a special requirement (i.e., an API call, but which you can solve with P / Invoke).

EDIT: (because you edited your q.)

The following code fragment shows how to get the address of a logical variable and how to convert this pointer to int(conversion to byteimpossible, we need four bytes).

unsafe
{
    // get pointer to boolean
    bool* boolptr = &mybool;                

    // get the int ptr (not necessary though, but makes following easier)
    int* intptr = (int*)boolptr;

    // get value the pointer is pointing at
    int myint = *intptr;

    // get the address as a normal integer
    int myptraddress = (int) intptr;
}

" 4 ". 4- , , . : # , ++. # CLR, , 4 32- , , . , , bool * ( bool) - , .

. , : , , .

+4

Source: https://habr.com/ru/post/1627531/


All Articles