• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# a trouble with memcpy..

## Recommended Posts

HellRiZZer    308
Hi guys, I have a trouble with memcpy. When I do the following thing:

fscanf(file, "TEMPLATE_ID: %d\n", &item_id);
CModel *mdl = new CMilkshape();

if(m_lstTemplateItems.at(item_id) !=NULL)
memcpy(mdl, m_lstTemplateItems.at(item_id), sizeof(CModel));
else
good_model = false;

if(!good_model)
delete mdl;
else
{
AllocateID(mdl);
m_lstObjects.push_back(mdl);
}
}

I erase all data when the program gets destroyed by calling delete in each member of vector list. (each m_lst is a kind of std::vector declaration, its checked and it works properly) So, when at the end I call m_lstTemplateItems.erase() that deletes all data inside the list, its goes OK. But when I call m_lstObjects.erase(), it gives me Debug Assertion failure error. Please help me to find out what''s wrong with my code. If there''s not enough info, I can post a link here with my project source code.. Thanks. " Do we need us? "

##### Share on other sites
Wavarian    850
Errr.. the 3rd param (sizeof()) is supposed to be the number of bytes you want to write to the memory address, not the size of what the memory address is representing.

or does

m_lstTemplateItems.at(item_id) return a CModel?

[edited by - Wavarian on May 27, 2003 10:03:39 AM]

##### Share on other sites
BriTeg    168
You shouldn't simply memcpy an object if that object contains pointers (or STL stuff like vectors, etc), because you're only copying the pointers, not what they point to. In other words, after the memcpy, both objects have pointers pointing to the same thing - so when one object is destroyed and deletes it's members, the memory pointed to is nuked, but since the other object also is pointing to it, when it goes to use or delete that same memory, boom.

Instead, what you want is to create copy and assignement constructors, where you manually allocate new mem for each object's member pointers and copy the memory of each member pointer individually.

[edited by - BriTeg on May 27, 2003 11:09:23 AM]

##### Share on other sites
Lawgiver    160

Yes, in that case, it would be better to design a copy constructor and to overload the ''='' operator as well.

Using memcpy with objects usually lead to trouble if the underlaying design of the object change (such as if the object use pointers inside, their content do not get copied, as BriTeg pointed out).

##### Share on other sites
Way Walker    745
quote:
Original post by BriTeg
You shouldn''t simply memcpy an object if that object contains pointers (or STL stuff like vectors, etc), because you''re only copying the pointers, not what they point to. In other words, after the memcpy, both objects have pointers pointing to the same thing - so when one object is destroyed and deletes it''s members, the memory pointed to is nuked, but since the other object also is pointing to it, when it goes to use or delete that same memory, boom.

Instead, what you want is to create copy and assignement constructors, where you manually allocate new mem for each object''s member pointers and copy the memory of each member pointer individually.

[edited by - BriTeg on May 27, 2003 11:09:23 AM]

I usually hear the terms "shallow copy" and "deep copy" when discussing this. Shallow copy is just a bit-for-bit copy, copying pointers but not copying what they point to. I think this would be appropriate for certain styles of collection classes (e.g. ones that adding an object adds a pointer to the object). Deep copy is what BriTeg describes, where what the pointers point to is copied.

Basically, know the difference and know which to use when. With shallow copy you just have to change your rules on who''s responsible for free''ing that pointer.

##### Share on other sites
HellRiZZer    308
Thanks to all for your replies, especially the BriTeg. As I''ve been debugging the code, I also noticed that the two models have the same pointer addresses inside the CMilkshape class. I actually realized that about 2 minutes after I posted here

So, you are suggesting to create a copy constructor? Not a problem

Thanks all for your time and help.

" Do we need us? "