Jump to content
  • Advertisement
Sign in to follow this  
Iccarus

c++ deleting

This topic is 4479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
You don't. delete is only for pointers that are allocated by new. EG[1] isn't even a pointer (or, at least, I assume it isn't).

Share this post


Link to post
Share on other sites
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
I don't think there's a way to do it if it's a single-dimension array. You'd need an array of pointers to do this.

Share this post


Link to post
Share on other sites
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
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 = 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 << std::endl;

delete[] strings[2];

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

for ( int i = 0; i < strnum; ++i )
delete[] strings;
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!