How would one get the size of pointed-to-data of a void pointer?
Is this possible in C?
How would one get the size of pointed-to-data of a void pointer?
Is this possible in C?
Through good and thoughtful programming on your part. You have to be careful when writing to ensure you cast the void* to a pointer of the correct type, or maybe you need just "remember" how many bytes the pointer is pointing to, depending on what you're doing.Originally posted by The Dog
How would one get the size of pointed-to-data of a void pointer?
Is this possible in C?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
void *v;
int i
v = &i;
printf("%d", sizeof( (int*)v ) );
Basicly you just use sizeof like normal. You, when using a void pointer, have to assume that whatever you're being told it actually points to, is in fact what it actually is.
Think of a void pointer as a union. Everything in the union actually resides in the same block of memory. However, by choosing one member of the union, you are in essence dereferencing the chunk of memory as whatever you want to use it as.
The same thing goes for void points. You dereference it as whatever you want to use it as. You just have to hope what you want to use it as, is actually what it really points to...
Quzah.
Hope is the first step on the road to disappointment.
i just wanted if it was possible to get the size of pointed-to-data
without having a clue of what it's pointing to.
I realize now that it's just a matter of guessing, but then again,
I doubt i'll take the chance.
thanx anyways