Memory leak ?

F

Felix Sima

Hi.

Does the following line generate a memory leak or doesn't ?

*** foo( new CTest( ) ); *** //See code below.

Watching the output of the program seems that only the constructor
of the temporary object is called so that the memory allocated for
"new CTest( )" doesn't get deallocated.

Is it the right behaviour? If not, what can I do ?

Thank you.
Felix

********* CODE *********
#include <iostream>
using namespace std;

class CTest
{
public:
CTest()
{
cout << "Constructor" << endl;
};
~CTest()
{
cout << "Destructor" << endl;
};
};

void foo( CTest *p )
{
};

int main( int argc, char **argv )
{
foo( new CTest( ) ); //Memory leak here ?

return 0;
};

******** OUTPUT ************
Constructor
 
P

Peter Koch Larsen

Felix Sima said:
Hi.

Does the following line generate a memory leak or doesn't ?

*** foo( new CTest( ) ); *** //See code below.

Watching the output of the program seems that only the constructor
of the temporary object is called so that the memory allocated for
"new CTest( )" doesn't get deallocated.

Is it the right behaviour? If not, what can I do ?

Thank you.
Felix

********* CODE *********
#include <iostream>
using namespace std;

class CTest
{
public:
CTest()
{
cout << "Constructor" << endl;
};
~CTest()
{
cout << "Destructor" << endl;
};
};

void foo( CTest *p )
{
};

int main( int argc, char **argv )
{
foo( new CTest( ) ); //Memory leak here ?

return 0;
};

******** OUTPUT ************
Constructor

Of course. For every new there should be a corresponding delete. Do you come
from Java? ;-)

/Peter
 
M

Martin SChukrazy

LOL what about C# or Visual Basic..
hahaha

Peter Koch Larsen said:
Of course. For every new there should be a corresponding delete. Do you come
from Java? ;-)

/Peter
 
R

Ron Samuel Klatchko

Felix Sima said:
Does the following line generate a memory leak or doesn't ?

*** foo( new CTest( ) ); *** //See code below.

It's not that specific line that generates a memory leak, but the
design of all your code. The problem isn't that you new it, but that
you have no matching delete.

samuel
 
K

Karl Heinz Buchegger

Felix said:
Hi.

Does the following line generate a memory leak or doesn't ?

*** foo( new CTest( ) ); *** //See code below.

Watching the output of the program seems that only the constructor
of the temporary object is called so that the memory allocated for
"new CTest( )" doesn't get deallocated.

Is it the right behaviour? If not, what can I do ?

Not doing dynamic allocation. Then you don't have to worry about freeing
it. ANd of course, not using pointers if there is no need to.

You could eg. do:

void foo( const CTest& Arg )
{
}

int main()
{
foo( CTest() );
}
 

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

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top