Advertisement

poperly deleting **+ pointer data

Started by May 06, 2001 10:14 PM
4 comments, last by EvilCrap 23 years, 9 months ago
for a dyamic 2d map, i found to use: char **Map; to properly delete this, i do this?: for y delete [] Map[y]; delete [] Map; ????
Depends how you declared it. If you declared it as an array, then you need to use delete[]
Advertisement
quote:
Original post by EvilCrap

for a dyamic 2d map, i found to use: char **Map;

to properly delete this, i do this?:

for y
delete [] Map[y];

delete [] Map;

????






that''s it exactly

Ok this is tricky.

I can declare an array of char pointers, which ends up being a **, but when I do a new for that one

char **pMap = new char*[50];

for instance,

that is not allocating any space for what the actual pointers in the array point to. All this says is that I have an array of 50 pointers. If I want to allocate space for each of the pointers, I have to do something like

for (int nCount=0; nCount<50; ++nCount)
{
pMap[nCount] = new char[128]; // for instance
}

Now I have an array of 50 pointers each one of which point to a buffer that has 128 chars.

I can''t delete the whole thing with one call. I have to do

for (int nCount=0; nCount<50; ++nCount)
{
delete [] pMap[nCount];}
}
delete [] pMap;

Don''t let the ** notation confuse you. It really just means ''array of pointers''

HTH

DSutherland


Also consider using C++ (stl) containers. They aren''t that hard to use really...

The fastest way (I think), which I use is to have a 1D array of tiles, which is w * h tiles long, then you access it like y * w + x; Plus, then you can just delete []. The multiply is the only thing that might be making it slower than the 2D one, but I think the extra memory access (you have to read the location map is pointing to, add [x] to that, read the value stored there, add [y] to that, then read the value there to get what you want) is slower than the mul. But maybe it would be slower since for 1D you have to load y and w, multiply them, load x and add it, load map and add it, then access your final value. But then you do get more cache hits since it''s stored linearly...

Anybody know which is really better?



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

This topic is closed to new replies.

Advertisement