PDA

View Full Version : sizeof char * and void *


binaryDigit
12-19-2002, 08:50 PM
storing user data with void *
user data will be of any type
char *, int, struct....

tried using sizeof() to malloc space for the void *, problem is char *'s have a size of 4 even when strlen is something like 20.

i either need to use something that will find the size of any data. char *, int , or struct...
or i need to be able to detect when a void * is pointing to a char *.

any ideas... ??

Strike
12-19-2002, 11:03 PM
Well, all pointers are going to be the same size when you sizeof() them. But that's to be expected. They are, after all, all pointers and they do all take up the same amount of space. So, if you truly are just storing the pointer, then 4 bytes is really all the space that it takes.

binaryDigit
12-20-2002, 10:25 AM
so how can i determine the size of memory pointed to by a void pointer.
i'm using memmove to copy the data to the void * location.

something like.

pass int to copy function as void *
allocate space for storage void *
copy data from passed void * to storage void *

is this clear? i don't know if what i'm saying makes sense.

i can't just let the storage void * point to the int. it won't work that way.

sedarious
12-20-2002, 10:43 AM
Unless you are going to track the size, your can't attain it. Thats one of the draw-backs of C/C++. This is also a reason many people use some the the STL stuffs.

binaryDigit
12-20-2002, 11:08 AM
ok thank you.