what is a void* pointer?

Started by
9 comments, last by CTRL_ALT_DELETE 23 years, 2 months ago
I have seen several pointers declared of type void* at this form. what exactly is a pointer to type void?
Advertisement
void*''s are often used in structures and stuff when you may need some extra memory for whatever. Then you can malloc() some memory of whatever data type you need and then free() it when you''re down. It''s very verstile because your not stuck with a single data type or a fixed amount of data.


"We are the music makers, and we are the dreamers of the dreams."
- Willy Wonka
A void* pointer is basically a pointer of indeterminate type, or an address without a type setting, however you want to look at it. This works because all pointers are the same size. Think about that for a minute... the data that the pointer points to may be of different sizes, but the actual physical size in memory of the pointer itself, which is just an address, has a fixed length dependent on operating system. Therefore the compiler lets you pass pure addresses using this mechanism. This is both a powerful and a scary thing, because you are required to make sense of what that pointer is pointing at when you reach the other side. It''s your responsibility to make sure things match up, and if they don''t, you''ll have memory overwrites and all sorts of nasty stuff going on, and you won''t get any warnings until it fatals.

-fel

~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Thank you both! I have benn wondering what that was all about for a few days. Now if i wanted to use a void pointer in a class or whatever, and wanted to put an int in it, would i allocate memory for the type of data I wanted to contain, i.e.

void* void_pointer;
void_pointer = new int;
*void pointer = 2;
delete void_pointer;

right?

Edited by - CTRL_ALT_DELETE on January 31, 2001 5:12:54 PM
I''m not a C++ guy by nature, but I''m pretty sure that is correct.
Just for the sake of it, the C equivelent would be:

void *void_pointer;

void_pointer = malloc(sizeof(int));

*void_pointer = 2;

free(void_pointer);

notably, I have missed out all error checking.

Also, on the subject - do I need any cast for the assignment operation (*void_pointer = 2'')?

-Mezz
Yeah, I guess so too that you''d have to byte cast it...
If so the compiler will tell you
cya,
Phil

Visit Rarebyte!
and no!, there are NO kangaroos in Austria (I got this questions a few times over in the states
Visit Rarebyte! and no!, there are NO kangaroos in Austria (I got this question a few times over in the states ;) )


Nope. Do not do that. It might work but it might make you soil your pants one day as well.

If you want an int:

int * temp=new int;
void* vptr=temp;

to get your int ptr back do the following:

int* temp2=static_cast(vptr);

or sometimes a reinterpret_cast(vptr); is required( I try to avoid casts).

Please for the sake of your underware, do not use void ptrs unless you NEED to. Void ptrs are really only usefull in Low level programming, like operating systems or memory managers.

If you don''t already know, learn about templates, and polymorphism, they will help you get rid of 99.999% of your void pointer requirements, and are type safe.

send me an email if you think I am a retard, or if you wan''t to tell my I am a genius(or anything in between) animade@mailcity.com
type entity[dynamic cmd_interface: cmd c, object o=0 [ if:c call_remote[c,o]; ]void;]
A good example of return a void* for a function is when you lock a DX surface. Depending on the bit depth of the surface, you may be getting a UCHAR, SHORT, or INT data type for the memory. For example, the lpSurface member in the DDSURFACEDESC2 structure is of type void* for this reason. Wden you lock the surface, you typecast the returned void pointer to whatever type you need.
hmm void pointers sound pretty interesting =)
Does pointer arithmetic work with them?
Well Mr Brown, you would hsve to type cast the void pointer first. i.e. you void pointer''s reference has no determined size without a cast. So the compiler would not know whether to add 1, 2, 4, 10, or whatever number of bytes to find the next reference.
That''s one of the dangers of void pointers.

By the way they are usually used in C for emulate concepts such as object oriented code a function.(Saw it in a book once at the library.)



It''s me, it''s that T-H-E-D.
If you missed a great moment in history don't worry, it'll repeat itself.

This topic is closed to new replies.

Advertisement