c++ deleting

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 on other sites
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 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 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 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 on other sites
I'm quite confident that you can't delete a single element within an array.

Share on other sites
Ok thanks, plan B then.

Share on other sites
Quote:
 Original post by IccarusHow 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 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[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 on other sites
example[1].~Flibble();new (&example[1]) Flibble(42,42);

The object is destroyed and rebuilt.
The array's memory isn't affected in any way.

Share on other sites
Quote:
 Original post by Frunyexample[1].~Flibble();new (&example[1]) Flibble(42,42);Use at your own risk.

For Beginners man! Think of the beginners!

Share on other sites
Quote:
 Original post by Frunyexample[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 on other sites
Quote:
 Original post by rip-offFor 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 on other sites
Quote:
Original post by Fruny
Quote:
 Original post by rip-offFor 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.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627682
• Total Posts
2978614

• 13
• 12
• 10
• 12
• 22