vector of vectors erase question

J

Jeff

I have a question about potential memory leakage in a 2 dim array I
have.

If I say:

vector< vector<int> > vec;

vec.push_back( vector<int>() ); // initialize the row
vec[0].push_back(1);
vec[0].push_back(2);
vec.pop_back();

This leaves 2 columns of leaked memory, correct? (vals 1 and 2)

Is it correct rather to say:

vec[0].pop_back(); // erase 2
vec[0].pop_back(); // erase 1
vec.pop_back();

My guess would be the latter is correct, however I had some conflicting
results while testing some simliar code and wanted to be sure.

Thanks
 
K

Kai-Uwe Bux

Jeff said:
I have a question about potential memory leakage in a 2 dim array I
have.

If I say:

vector< vector<int> > vec;

vec.push_back( vector<int>() ); // initialize the row
vec[0].push_back(1);
vec[0].push_back(2);
vec.pop_back();

This leaves 2 columns of leaked memory, correct? (vals 1 and 2)

Not correct. There is no memory leak. The line

vec.pop_back();

destroys the vector object vec[0]. The destructor of this opbject, in turn,
destroys the two entries vec[0][0] and vec[0][1] and deallocates their
memory.

Is it correct rather to say:

vec[0].pop_back(); // erase 2
vec[0].pop_back(); // erase 1
vec.pop_back();

You could do that, too.
My guess would be the latter is correct, however I had some conflicting
results while testing some simliar code and wanted to be sure.

Huh?


Best

Kai-Uwe Bux
 
B

Bo Persson

Jeff said:
I have a question about potential memory leakage in a 2 dim array I
have.

If I say:

vector< vector<int> > vec;

vec.push_back( vector<int>() ); // initialize the row
vec[0].push_back(1);
vec[0].push_back(2);
vec.pop_back();

This leaves 2 columns of leaked memory, correct? (vals 1 and 2)

No. The vector will take care of its own storage.

When vec goes out of scope, any remaning elements in the vector will
be destroyed, and the vectors memory will be released. You don't have
to do anything.
Is it correct rather to say:

vec[0].pop_back(); // erase 2
vec[0].pop_back(); // erase 1
vec.pop_back();

My guess would be the latter is correct, however I had some
conflicting
results while testing some simliar code and wanted to be sure.

That will destroy the objects stored in the vector. It doesn't affect
the memory space used by the vector. That is handled automatically by
the vector.


Bo Persson
 
T

TB

Jeff skrev:
I have a question about potential memory leakage in a 2 dim array I
have.

If I say:

vector< vector<int> > vec;

vec.push_back( vector<int>() ); // initialize the row
vec[0].push_back(1);
vec[0].push_back(2);
vec.pop_back();

This leaves 2 columns of leaked memory, correct? (vals 1 and 2)

Is it correct rather to say:

vec[0].pop_back(); // erase 2
vec[0].pop_back(); // erase 1
vec.pop_back();

My guess would be the latter is correct, however I had some conflicting
results while testing some simliar code and wanted to be sure.

If you don't use 'new' or 'new[]', then you don't have to use 'delete'
or 'delete[]', and thus don't have to worry about memory leaks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top