开发者

What is void* and to what variables/objects it can point to

开发者 https://www.devze.com 2023-02-13 17:30 出处:网络
Specifically, can it point to int/float etc.? What about开发者_C百科 objects like NSString and the like?

Specifically, can it point to int/float etc.? What about开发者_C百科 objects like NSString and the like? Any examples will be greatly appreciated.


void* is such a pointer, that any pointer can be implicitly converted to void*.

For example;

int* p = new int;
void* pv = p; //OK;
p = pv; //Error, the opposite conversion must be explicit in C++ (in C this is OK too)

Also note that pointers to const cannot be converted to void* without a const_cast

E.g.

const int * pc = new const int(4);
void * pv = pc; //Error
const void* pcv = pc; //OK

Hth.


In C any pointer can point to any address in memory, as the type information is in the pointer, not in the target. So an int* is just a pointer to some memory location, which is believed to be an integer. A void* pointer, is just a pointer to a memory location where the type is not defined (could be anything).

Thus any pointer can be cast to void*, but not the other way around, because casting (for example) a void pointer to an int pointer is adding information to it - by performing the cast, you are declaring that the target data is integer, so naturally you have to explicitly say this. The other way around, all you are doing is saying that the int pointer is some kind of pointer, which is fine.

It's probably the same in C++.


A void * can point at any data-like thing in memory, like an integer value, a struct, or whatever.

Do note, however, that you cannot freely convert between void * and function pointers. This is because on some architectures, code is not in the same address space as data, and thus it's possible that address 0x00000000 for code refers to a different set of bits than address 0x00000000 for data does.

It would be possible to implement the compiler so that void * is large enough to remember the difference, but in general I think this is not done, instead the language leaves it undefined.

On typical/mainstream computers, code and data reside in the same address space, and then the compilers typically generate sensible results if you do store a function pointer into a void *, since it can be quite useful.


Besides everything else that was already said by the other users, a void* it's commonly used in callback definitions. This allows your callback to receive user data of any type, including your own defined objects/structs, which should be casted to the appropriate type before using it:

void my_player_cb(int reason, void* data)
{
    Player_t* player = (Player_t*)data;

    if (reason == END_OF_FILE)
    {    
        if (player->playing)
        {
          // execute stop(), release allocated resources and 
          // start() playing the next file on the list
        }
    }
}


void* can point to an address in memory but the syntax has no type-information. you can cast it to any pointer-type you want but it is your responsibility that that type matches the semantics of the data.

0

精彩评论

暂无评论...
验证码 换一张
取 消