Sign in to follow this  
Iccarus

c++ deleting

Recommended Posts

Iccarus    122
How would I go about deleting just one part of something created using new. E.g.
EG* example = new EG[3];

example[0].Flibble(foo,bar); // the variables aren't important
example[1].Flibble(bar,foo);
example[2].Flibble(18,12);

If I just want to delete one of those 3 how would I do it? delete [1] example?

Share this post


Link to post
Share on other sites
JohnBolton    1372
You can't. If you want to delete them separately, you must new them separately. Perhaps like this:
    EG** example = new EG *[3];

example[0] = new EG;
example[1] = new EG;
example[2] = new EG;

example[0]->Flibble(foo,bar);
example[1]->Flibble(bar,foo);
example[2]->Flibble(18,12);

delete example[0];
delete example[1];
delete example[2];

delete[] example;
Want do you want to do? Maybe there is a better way.

Share this post


Link to post
Share on other sites
Iccarus    122
Using that example I can do:

delete [] example

to delete the whole thing. Is there anyway to completly clear one of them from memory without disturbing the rest of them?

I don't think there is I'm just making sure.

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by Iccarus
How would I go about deleting just one part of something created using new.

E.g.

*** Source Snippet Removed ***

If I just want to delete one of those 3 how would I do it?

delete [1] example?


std::vector<EG> example(3);

example[0].Flibble(foo,bar); // the variables aren't important
example[1].Flibble(bar,foo);
example[2].Flibble(18,12);

example.erase( example.begin() + 1 );

[grin]

Share this post


Link to post
Share on other sites
tychon    652
An example of this would be:

#include <iostream>
#include <cstring>

int main()
{
const int strnum = 3;
char** strings = new char*[strnum];

for ( int i = 0; i < strnum; ++i )
strings[i] = new char[1024];

strcpy( strings[0], "this is a string" );
strcpy( strings[1], "this is another string" );
strcpy( strings[2], "this is the last string" );

for ( int i = 0; i < strnum; ++i )
std::cout << strings[i] << std::endl;

delete[] strings[2];

for ( int i = 0; i < strnum; ++i )
std::cout << strings[i] << std::endl;

for ( int i = 0; i < strnum; ++i )
delete[] strings[i];
delete[] strings;
}


Ignore the fact that this isn't safe, heh.

Of course, things don't get any prettier in this method. You really should be using an std::vector or other such container.

Share this post


Link to post
Share on other sites
Fruny    1658
example[1].~Flibble();
new (&example[1]) Flibble(42,42);


Use at your own risk.
The object is destroyed and rebuilt.
The array's memory isn't affected in any way.

Share this post


Link to post
Share on other sites
tychon    652
Quote:
Original post by Fruny
example[1].~Flibble();
new (&example[1]) Flibble(42,42);


Use at your own risk.


That's so frightening my ramsticks are quaking in their sockets.

Share this post


Link to post
Share on other sites
Fruny    1658
Quote:
Original post by rip-off
For Beginners man! Think of the beginners!


Hey, he wanted a loaded gun, I gave him a loaded gun.
How was I supposed to tell he was only 9?

Share this post


Link to post
Share on other sites
nobodynews    3126
Quote:
Original post by Fruny
Quote:
Original post by rip-off
For Beginners man! Think of the beginners!


Hey, he wanted a loaded gun, I gave him a loaded gun.
How was I supposed to tell he was only 9?


Okay, that's probably the best quote ever[/exageration]. I'm gonna make that my signature.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this