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);
c++ deleting
How would I go about deleting just one part of something created using new.
E.g.
If I just want to delete one of those 3 how would I do it?
delete [1] example?
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.
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.
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.
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.
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:
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.
#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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement