Thursday, 29 December 2016

c: size of void*



I'm a bit confused with a void* pointer in C. Especially after reading this question: Is the sizeof(some pointer) always equal to four?, where one person says there is no guarantee that sizeof(int *) == sizeof(double *)



My question is: is there a guarantee of sizeof(void*) >= sizeof(any other pointer type)?

In other words, can I always assign a some_type* pointer to a void* pointer and then get it back as some_type*?


Answer



Only data pointers. void * can hold any data pointer, but not function pointers.



Here is a C FAQ.




void *'s are only guaranteed to hold object (i.e. data) pointers; it
is not portable to convert a function pointer to type void *. (On some
machines, function addresses can be very large, bigger than any data

pointers.)




As for the first part, yes, different types can have pointers of different sizes:


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...