473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is there a memory leak in this code ?

// -----------------------------------

class Column
{
public:
string name;
vector<int values;
};

// -----------------------------------

void loadValues()
{
Column p = new Column();

p->values.push_back(55); // <--- Line 1
p->values.push_back(66); // <--- Line 2

delete p; // <--- Line 3
}

// -----------------------------------

Are the values inserted (Line 1 and 2) on
the stack or on the heap ?

Is there a memory leak for the two inserted
values inspite of the "delete p" at line 3 ?

Thanks
Diwakar
Dec 28 '07 #1
6 1522
Diwa wrote:
// -----------------------------------

class Column
{
public:
string name;
vector<int values;
};

// -----------------------------------

void loadValues()
{
Column p = new Column();
You probably meant

Column *p = new Column();
>
p->values.push_back(55); // <--- Line 1
p->values.push_back(66); // <--- Line 2

delete p; // <--- Line 3
}

// -----------------------------------

Are the values inserted (Line 1 and 2) on
the stack or on the heap ?
Values that appear in the code (the literals '55' and '66')
are usually neither in the stack nor in the heap (free store)
since they are literals, they are usually in the code (parts
of the instruction that places them where arguments are
transferred to the function). Values that the container
'p->values' stores are _always_ in the free store, the vector
allocates all its values there, unless you provide some kind
of custom allocator, which you didn't.
Is there a memory leak for the two inserted
values inspite of the "delete p" at line 3 ?
No.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 28 '07 #2
On Dec 28, 2:15*pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Diwa wrote:
// -----------------------------------
class Column
{
*public:
* *string * * * *name;
* *vector<int* values;
};
// -----------------------------------
void loadValues()
{
* * Column *p = new Column();
*p->values.push_back(55); * // <--- Line 1
*p->values.push_back(66); * // <--- Line 2
*delete p; * * * * * * * * *// <--- Line 3
}
// -----------------------------------
Are the values inserted (Line 1 and 2) on
*the stack or on the heap ?

Values that the container
'p->values' stores are _always_ in the free store, the vector
allocates all its values there, unless you provide some kind
of custom allocator, which you didn't.
Is there a memory leak for the two inserted
*values inspite of the "delete p" at line 3 ?

No.
I suspect there is a memory leak. Here is my reasoning.

When "Column *p = new Column( )" is done it allocates,
lets say, 20 bytes. Just before the memory address
returned by "new", maybe it stores the num of bytes.

The "push_back()" done later at line 1 and line 2 may
results in some more "new" but still it will not
change the value (num of bytes) just before addr "p"

So at line 3, when "delete p" executes, it sees the
velue "20" just before p and then deletes only 20
bytes.

Am I missing something ?
Dec 28 '07 #3
On 2007-12-28 20:32, Diwa wrote:
On Dec 28, 2:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>Diwa wrote:
// -----------------------------------
class Column
{
public:
string name;
vector<int values;
};
// -----------------------------------
void loadValues()
{
Column *p = new Column();
p->values.push_back(55); // <--- Line 1
p->values.push_back(66); // <--- Line 2
delete p; // <--- Line 3
}
// -----------------------------------
Are the values inserted (Line 1 and 2) on
the stack or on the heap ?

Values that the container
'p->values' stores are _always_ in the free store, the vector
allocates all its values there, unless you provide some kind
of custom allocator, which you didn't.
Is there a memory leak for the two inserted
values inspite of the "delete p" at line 3 ?

No.

I suspect there is a memory leak. Here is my reasoning.

When "Column *p = new Column( )" is done it allocates,
lets say, 20 bytes. Just before the memory address
returned by "new", maybe it stores the num of bytes.

The "push_back()" done later at line 1 and line 2 may
results in some more "new" but still it will not
change the value (num of bytes) just before addr "p"

So at line 3, when "delete p" executes, it sees the
velue "20" just before p and then deletes only 20
bytes.

Am I missing something ?
Yes, when you push back the numbers on line 1 and 2 those are stored in
memory managed by the vector 'values'. When you, on line 3, delete p it
will call the destructor of the Column class pointed to by p. When this
happens it will call the destructors of its members 'name' and 'value'.
When the vector's destructor is run it will free whatever memory was
used by the vector, including that used to store the elements that you
pushed back on line 1 and 2. You only have to worry about the memory you
explicitly allocated using new, nothing else.

--
Erik Wikström
Dec 28 '07 #4
On Dec 28, 3:23*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-12-28 20:32, Diwa wrote:


On Dec 28, 2:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Diwa wrote:
// -----------------------------------
class Column
{
*public:
* *string * * * *name;
* *vector<int* values;
};
// -----------------------------------
void loadValues()
{
* * Column *p = new Column();
*p->values.push_back(55); * // <--- Line 1
*p->values.push_back(66); * // <--- Line 2
*delete p; * * * * * * * * *// <--- Line 3
}
// -----------------------------------
Are the values inserted (Line 1 and 2) on
*the stack or on the heap ?
Values that the container
'p->values' stores are _always_ in the free store, the vector
allocates all its values there, unless you provide some kind
of custom allocator, which you didn't.
Is there a memory leak for the two inserted
*values inspite of the "delete p" at line 3 ?
No.
I suspect there is a memory leak. Here is my reasoning.
When "Column *p = new Column( )" is done it allocates,
* lets say, 20 bytes. Just before the memory address
* returned by "new", maybe it stores the num of bytes.
The "push_back()" done later at line 1 and line 2 may
* results in some more "new" but still it will not
* change the value (num of bytes) just before addr "p"
So at line 3, when "delete p" executes, it sees the
* velue "20" just before p and then deletes only 20
* bytes.
Am I missing something ?

Yes, when you push back the numbers on line 1 and 2 those are stored in
memory managed by the vector 'values'. When you, on line 3, delete p it
will call the destructor of the Column class pointed to by p. When this
happens it will call the destructors of its members 'name' and 'value'.
When the vector's destructor is run it will free whatever memory was
used by the vector, including that used to store the elements that you
pushed back on line 1 and 2. You only have to worry about the memory you
explicitly allocated using new, nothing else.
Ah, now I feel stupid. Obviously, when 'delete p' is done it
will invoke the dtor of "Column" class in which case all
memory deletion is taken care of automatically. I don't why
but my brain was processing the behaviour of "delete p" as
behaviour of "free p". Thanks anyways, Erik and Victor.
Dec 28 '07 #5
Victor Bazarov wrote:
> Column p = new Column();

You probably meant

Column *p = new Column();
What they probably really wanted is
Column p;
p.values.push_back...

It's the Java syndrome. You do not "new" everything
in C++.
Dec 28 '07 #6
Diwa wrote:
// -----------------------------------

class Column
{
public:
string name;
vector<int values;
};

// -----------------------------------

void loadValues()
{
Column p = new Column();

p->values.push_back(55); // <--- Line 1
p->values.push_back(66); // <--- Line 2

delete p; // <--- Line 3
}

// -----------------------------------

Are the values inserted (Line 1 and 2) on
the stack or on the heap ?
They are in free storage (you probably would call that the heap) since you
used the standard allocator with the vectors.

Is there a memory leak for the two inserted
values inspite of the "delete p" at line 3 ?
The memory will leak when Line 1 or Line 2 throws an exception (e.g., due to
reallocation). Otherwise, there is no leak.
Best

Kai-Uwe Bux
Dec 29 '07 #7

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

Similar topics

10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
17
by: Mike | last post by:
Hello, I have following existing code. And there is memory leak. Anyone know how to get ride of it? function foo has been used in thousands places, the signature is not allowed to change. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.