I have a map of pointers to objects, i want to delete some randomly chosen pointers and the objects they point to. Is it possible to do it without using libraries like boost ?
I assign the map as follows

map<int,particle*> particle_map;
particle* particles= new particle[number_of_particles];
for(i==0;i<number_of_particles;i++)
{
   particle_map[i] =   &particles[i];
}

now how to delete the key and the the object to which the pointer points ??

Recommended Answers

All 6 Replies

You can erase an element from the map, but you can't delete just one of the pointers because it belongs to an array. If you allocate the pointers one at a time then you could delete them in any order you want.

map<int,particle*> particle_map;
for(i==0;i<number_of_particles;i++)
{
   particle_map[i] =   new particle;
}

THANK YOU ....!!
but is it possible to declare a non-dynamic array with its size decided at runtime ??

That would be a contradiction in terms; an array whose size is decided at runtime is a dynamic array.

If you mean you'd like a dynamic array where you don't need to worry about explicit memory management, then the STL vector is C++'s "dynamic array".

edit: But having seen what you're trying to do, that would cause more problems than it would solve.
Why are you trying to use a map to delete some objects which that map is not responsible for? The obvious solution would be to redesign your program so that the map itself is responsible for the objects, not the array.

I come before you, to stand behind you,
To tell you something I know nothing about.
Next Thursday, which is Good Friday,
There will be a mothers' meeting for fathers only.

That would be a contradiction in terms; an array whose size is decided at runtime is a dynamic array.

If you mean you'd like a dynamic array where you don't need to worry about explicit memory management, then the STL vector is C++'s "dynamic array".

I mean like :

cin>>n>>m;
const int  N= n/m;
int array[N];

a statically allocated array is "statically allocated" because its size is known by the compiler (its size is static - static means that it can't and won't change)

The compiler will ensure that a statically allocated array is created with a fixed size.

However, the compiler has absolutely no control over anything which is not known at compile time; this includes concepts such as memory addresses and user input, therefore there is no such thing as a statically allocated array whose size is known at runtime - its a pure contradiction


if you wish to create an array at runtime, then the compiler doesn't know how much space you need, therefore you must manually allocate and clean up memory yourself, or use a container which will handle memory at runtime (And is free to shuffle data around in memory for you, so maintaining pointers to elements stored in a container is undefined behaviour).

Perhaps you could shed some light on exactly what you're trying to do in 'real' terms? i.e. what problem are you trying to solve?

I solved it. Thanks for ur help. Can u help me in this thread ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.