PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Is there a memory leak in this code ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Is there a memory leak in this code ?

Réponse
 
LinkBack Outils de la discussion
Vieux 28/12/2007, 18h57   #1
Diwa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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
  Réponse avec citation
Vieux 28/12/2007, 19h15   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a memory leak in this code ?

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


  Réponse avec citation
Vieux 28/12/2007, 19h32   #3
Diwa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a memory leak in this code ?

On Dec 28, 2:15pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> 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 ?
  Réponse avec citation
Vieux 28/12/2007, 20h23   #4
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a memory leak in this code ?

On 2007-12-28 20:32, Diwa wrote:
> On Dec 28, 2:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> 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
  Réponse avec citation
Vieux 28/12/2007, 20h40   #5
Diwa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a memory leak in this code ?

On Dec 28, 3:23pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2007-12-28 20:32, Diwa wrote:
>
>
>
>
>
> > On Dec 28, 2:15 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> >> 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.
  Réponse avec citation
Vieux 28/12/2007, 23h51   #6
Ron Natalie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a memory leak in this code ?

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++.
  Réponse avec citation
Vieux 29/12/2007, 00h40   #7
jkherciueh@gmx.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Is there a memory leak in this code ?

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h08.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,18838 seconds with 15 queries