# Deleting Pointer Arrays, but not the whole array

## Recommended Posts

Cornstalks    7030
Is there a way to delete pointer arrays without deleting the entire array? For example, say you have a pointer array called Array[50]. Now say you don't need Array[35] anymore. Is there away to delete only Array[35]? Well, thanks for reading. --Michael

##### Share on other sites
SiCrane    11839
Assuming C++, I'd use a std::vector, then you can use erase() to remove the 36th element.

##### Share on other sites
DJHoy    378
Not without rebuilding the entire array... probably what you're looking for is std::list (the STL linked list classes) in C++ or, java.util.List for java...

##### Share on other sites
rip-off    10976
Quote:
 Original post by MikeTacularIs there a way to delete pointer arrays without deleting the entire array?For example, say you have a pointer array called Array[50].Now say you don't need Array[35] anymore. Is there away to delete only Array[35]?Well, thanks for reading.--Michael

so

void func(){Type *Array[50];for( int i = 0 ; i < 50 ; i++ )   Array[i] = some_pointer_generator(i);// ok now we no longer need 35delete Array[35]; // c++free( Array[35] ); // cArray[35] = NULL; // mark it as unused}

you could optionally move all the elements back so the gap is at the end, but a std::vector would probably be what you want if your going to start doing things like that

##### Share on other sites
Cornstalks    7030
Well thanks everyone. Sorry, I should have said I was using c++, which I am. I haven't exactly figured out the STL, for a beginner I find it very confusing. I'll try looking at it though.

##### Share on other sites
AAAP    137
This looks like a job for vectors! as stated before...

it's easy and fun.

vector<your type> name; (or name() if theres a constructor)

then you can do vector.reserve(name.end(), 50 million, type)

or push/pop operations, you can clear the list, whatever you want. its really dynamic. only problem is, making multidimensional vectors is a bit complicated. I was reaidng Dr GUI's article about it on a MSDN archive and i was totally lost :(

[Edited by - AAAP on November 27, 2005 8:43:08 PM]