473,473 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

vector of vectors erase question

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

Mar 5 '06 #1
3 6226
Jeff wrote:
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
Mar 5 '06 #2

"Jeff" <je**@rahul.net> skrev i meddelandet
news:11*********************@i40g2000cwc.googlegro ups.com...
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
Mar 5 '06 #3
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.

--
TB @ SWEDEN
Mar 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
9
by: david wolf | last post by:
I want to delete all even numbers in a vector, I am not sure if there's any better way to do it. Following program is how I did it. Look at the part of code beginning from comments: //delete all...
8
by: He Shiming | last post by:
Hi, I've developed a class that implements an interface definition. It looks like this: class IRecord { public: // define interface methods by pure virtual methods // no member variables }
2
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
3
by: Aarti | last post by:
I was reading about exception safety in vectors and came across a staement that says The clear operation of vector guarantees a nothrow provided the copy and assignment operator do not throw an...
5
by: Anil | last post by:
I am facing problem while erasing an elemet from stl vector when its size is 2. It works fine when SIZE 2. Can anybody help me in this?? Following is the sample code which i tried. #include...
5
by: Boltar | last post by:
Hi Is there a way of inserting and erasing to/from a vector using an array index instead of an iterator or alternatively a way to map an index number to an iterator? eg: vector<intv;
5
by: vsachar | last post by:
Hi I had a doubt about deleting from vectors. I have three vectors of types: void*, bool and int. I insert into these three vectors together and when the vector of bool type has the value true, I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.