Skip to main content
GameDev.net gamedev.net
🔒 Locked

Returning a VOID*

Started by GekkoCube Feb 22, 2003 at 10:15 AM 9 replies 250+ views
Original Post
GekkoCube
GekkoCube
...returning a VOID* ... what does it do and how can i use it for my advantage?
glassJAw
glassJAw
It returns a pointer that you can cast to any type. It''s generally not good practice to use VOID* but it has its uses.
Ready4Dis
Ready4Dis
A void * is simply a pointer to an unknown type. It can be useful for things like memory allocation, copying, etc.

Say I want to make a function to Allocate and Free memory... voids work good, since you don''t know what kind of object you are allocating for..

void *AllocateMem(unsigned long Size)
{
void *MemToAllocate = new unsigned char[Size];
return MemToAllocate;
}

void Delete(void *Memory)
{
delete Memory;
}

or for copying memory irregardless of what''s in it!

void CopyMem(void *Dst, void *Src, unsigned long Size)
{
char *cDst = Dst, *cSrc=Src;
while (size--)
(*cDst++)=(*cSrc++);
}

This could be used to copy arrays of any type, or custom structs, etc, without having to know it''s memory type.

A better way to accomplish this would probably be using templates, but back in the day of C, this was the way to do anything that was type independant (and it''s still more efficient size wise to use this instead of templates, because a template function would actually create mutliple copies of the function for each data type that needed it, while this one is just a single function that will work with any data types).
dalleboy
dalleboy
quote:
Original post by Ready4Dis
void *AllocateMem(unsigned long Size)
{
void *MemToAllocate = new unsigned char[Size];
return MemToAllocate;
}
void Delete(void *Memory)
{
delete Memory;
}


That would get you into trouble, you have some options:

1) Use malloc and free for void* allocation and deallocation.

2) Still use new unsigned char[Size], but you need to cast your void* pointer into an unsigned char* before deletion (and don't forget to delete it as an array):

void Delete(void *Memory)
{
delete [] reinterpret_cast<unsigned char*>(Memory);
}


3) Something different.



Update GameDev.net system time campaign - success at last

[edited by - dalleboy on February 22, 2003 1:47:02 PM]
Zipster
Zipster
Nowadays, you'll want to try and get around void pointer ambiguity using a template instead. It's one of they're primary uses, as defined by MSDN at least. It's much more type-safe, which is something that C++ does a lot better than C.

For example, the above functions would work best with a template. You won't have to worry about getting it casted to the right type all the time, becase the compiler generates all the type-specific code for you! Now isn't that something!

[edited by - Zipster on February 22, 2003 6:43:57 PM]
GekkoCube
GekkoCube
Zipster, you mean make my base Object class into a template class or make my vector list of Objects into a template list?

I''ll use templates, just start me off - thanks!
Zipster
Zipster
I can''t tell you whether or not a template would help you in this situation because I don''t what your code is like, but you would want to consider the function that returns a void pointer for templating. Once again, it all depends. Templates aren''t a universal solution

Maybe if you post the function that returns the void pointer I can lead you in a good direction.
GekkoCube
GekkoCube

  
// This is in my CObject.h file (base)

virtual VOID* GetObject()
{
return 0;
}

// This is in CTerrain.h file, (derives from CObject)

VOID* GetObject()
{
return this;
}


But i think the problem is my vector list of CObject pointers is a member in a "manager" class called CEngine.h
So this is how i TRY to get an object from the list of objects...

  
VOID* GetObject( int index )
{
return m_vObjects[index];
}


Of course, i tried this, and it doesnt work, i get an error when i try to use it the way i think its suppose to work...

  
engine->GetObject( index )->SetCamerePos( camera->Pos );
GekkoCube
GekkoCube
the error is this:

error C2227: left of ''->SetCamerePos'' must point to class/struct/union
civguy
civguy
Gekko, void isn''t really a type. The compiler can''t possibly know what kind of memory a void pointer is pointing at. You need to tell it to the compiler. So return CObject pointer instead.

  
CObject* GetObject( int index ) {
return m_vObjects[index];
}
And why do you spell VOID by the way? ''void'' with lowercase letters is the correct keyword.
DerekSaw
DerekSaw
Just to add... make sure you really knows the ''type'' returned by void* because delete-ing it won''t call the appropriate destructor.



When you choose technology, you have to ignore what other people are doing, and consider only what will work the best.
"after many years of singularity, i'm still searching n the event horizon" o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.