   Copying arrays by assignment (C++)
  Is this a memory leak?Map::Map(int size_in){    size = size_in;    map = new GLubyte[size];};void Map::updateMap(){    GLubyte* temp_map = new GLubyte[size];    /*    Fill up the temp_map based on original map values    */    map = temp_map;    delete temp_map;};[Edited by - PrestoChung on June 10, 2010 2:32:17 AM]
  Yes. When updateMap() is called after Map(int) was called, you write to map, and thus lose the information gained in new GLubyte[size];, and are not able to delete that chunk of memory anymore without magick voodoo outside the scope of std-C++.   ;  Would delete temp_map then delete map as well since after  map = temp_map they should have the same address?I'm trying to animate a 2d bitmap as a cellular automata but need to preserve the original map while I check neighbor values before I can overwrite it.   ;  WEll, yes, it would free the memory that both map and temp_map point to. Nevertheless, like phresnel said, the memory that you accquired with map = new GLubyte[size];would still be lost since *nothing* points to it anymore, so there is no way of deleting it.   ;  Maybe this helps: delete won't delete the pointer-variable, but the chunk of memory to which the pointer points. The pointer-variables itself remain unchanged. So after delete, both of your pointers show to a no-longer-existent chunk of memory. And no one is pointing to your firstly allocated chunk, which is still there, anymore.   ;  Solution?void Map::updateMap(){    GLubyte* temp_map = new GLubyte[size];    /*    Fill up the temp_map based on original map values    */    for (int i = 0; i < bytes_total; i++){   map = temp_map;    }    delete temp_map;};;  Make sure that you don't access elements out of array bounds (better use the same size variables for all allocations and in the loop), then, yes, correct :)   ;  Fill the original map data with temp map as you do temp map with original.Quote: /* Fill up the temp_map based on original map values */ ;Quote:Original post by phresnelMake sure that you don't access elements out of array bounds (better use the same size variables for all allocations and in the loop), then, yes, correct :)Oops, that is the same variable I had just changed it to size for example.Kasya:I want to wait until it is all done before it is overwritten because the surface will be boundless ie neighbors will wrap on edges and so be in differently separated bytes.;  Important detail: you should use delete [] when you allocate something with new [], and delete when you use new.   