c++ deleting

Started by
12 comments, last by nobodynews 18 years ago
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?
Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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).
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.
Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
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.
I'm quite confident that you can't delete a single element within an array.
Ok thanks, plan B then.
Unless I say otherwise assume I'm talking bout c++. Make things a lot easier :D
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]
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.
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement