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

Copying based on ugly void pointers

Started by emileej Jul 18, 2003 at 1:38 AM 7 replies 350+ views
Original Post
emileej
emileej
I have two void pointers: void *val; void *tmp; I also know which type is hidden in the val pointer: if(typ==TYPE_BOOL) //... else if(typ==TYPE_CHAR) //... else if(typ==TYPE_INT) //... else //must be string //... What I need to do is to copy the object, val is pointing at, and make tmp point at the copy - using new so that I can later use delete to get rid of it. I have tried memcpy and other less nice things, but nuthing works. Quite possibly I am just using memcpy wrongly or maybe I''m on the totaly wrong approach here? ------------- E-)mil "I feel like... I feel like... Like taking over the world!" - Day of the Tentacle
Emil Johansen- SMMOG AI designerhttp://smmog.com
Beer Hunter
Beer Hunter
if (typ == TYPE_BOOL)
    tmp = new bool(*static_cast<bool*>(val));


Repeat for other types.

[edited by - Beer Hunter on July 18, 2003 3:07:35 AM]
emileej
emileej
Wee - this works great on bool, int and char. Earlier I had tried this casting nightmare:
	new int(*((int *)val));  

But it less than worked.
1) However this new method doesn't seem to be working on my string type values (basic_string).
2) Another bug - void *tmp is in fact:
	vals; //vals is a void **  

And I'm having dificulties deleting this on program exit. This works:
	delete[] vals;  

And this does not:
	int i=0;
while(vals[i])
delete vals[i++];
delete[] vals;

I thought that if I used the first method in stead of the second - I'd leave a lot of occupied mamory behind, but perhaps delete[] really takes care of this?

[edited by - emileej on July 18, 2003 3:27:59 AM]
Emil Johansen- SMMOG AI designerhttp://smmog.com
Beer Hunter
Beer Hunter
On the first issue, it compiles and runs for me, so the only thing I can think of is if you''ve written static_cast<basic_string<...>> — in this case, you need to add a space between the two greater-than symbols. If this isn''t the case, post the relevant code.

On the second issue, you need to cast vals[i] to the correct type before deleting it (otherwise, the program wouldn''t know when to call the destructors of your basic_strings).
emileej
emileej
1) This is the line for copying the basic string:
vals[col-1]=new string(*static_cast(val));
2) This is my modified delete routine which still crashes the program:

if(vals){
int i=0;
CDBCol *tmp=firstcol;//firstcol is the first element in a linked list which I use to keep track of the types

while(vals[i]){
if(tmp->IsBool())
delete (static_cast<bool*>(vals[i++]));
else if(tmp->IsChar())
delete (static_cast<char*>(vals[i++]));
else if(tmp->IsInt())
delete (static_cast<int*>(vals[i++]));
else
delete (static_cast<string*>(vals[i++]));
tmp=tmp->GetNext();
}
delete[] vals;
}
Emil Johansen- SMMOG AI designerhttp://smmog.com
Beer Hunter
Beer Hunter
Your code for copying the string is fine. The error must be somewhere else — this sort of thing can be annoying to track down...

In the deletion routine, I see 'while (vals[i])'. Unless you've explicitly put a null pointer after the rest of the values, that looks like something which'll crash.

[edited by - Beer Hunter on July 18, 2003 4:20:13 AM]
emileej
emileej
I replaced that condition with while(tmp) as I enshure that the last element in the list returns 0 when GetNext() is called. However the error is still there...
Emil Johansen- SMMOG AI designerhttp://smmog.com
risingdragon3
risingdragon3
If you're not doing this for learning may I suggest that you try boost::any in conjunction with any container type? Since you already know the type information inside the "any" type you will find boost::any will do all the assignment and memory management for you and hide the ugly casting.

As to the memory leak it could be anything Good luck...
it could be possible you're assignment stack allocated objects onto the heap?

[edited by - risingdragon3 on July 18, 2003 5:07:55 AM]
emileej
emileej

MessageBox(NULL,"Deleting values...","Debug",MB_OK);
if(vals){
int i=0;
CDBCol *tmp=firstcol;
while(tmp){
string str="Value ";
str+=''0''+i;
MessageBox(NULL,str.c_str(),"Debug",MB_OK);
/*[BUGGY BLOCK]
Dies when deleting the _last_ element.
I have tried with diff. numbers of elements,
but its allways the last element.
The funny thing is that at another point
in my code - I can without trouble use delete
on my void* elements...*/

if(tmp->IsBool())
delete (static_cast<bool*>(vals[i]));
else if(tmp->IsChar())
delete (static_cast<char*>(vals[i]));
else if(tmp->IsInt())
delete (static_cast<int*>(vals[i]));
else
delete (static_cast<string*>(vals[i]));
/*[/BOGGY BLOCK]*/
MessageBox(NULL,"Fetching next","Debug",MB_OK);
tmp=tmp->GetNext();
i++;
}
MessageBox(NULL,"Deleting array","Debug",MB_OK);
delete[] vals;
}
MessageBox(NULL,"Destructor complete","Debug",MB_OK);
Emil Johansen- SMMOG AI designerhttp://smmog.com

Topic Locked

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

Sign in to reply to this topic.