C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

will it cause memory leak?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Rayer
Guest





PostPosted: Sun Jul 02, 2006 5:08 pm    Post subject: will it cause memory leak? Reply with quote



consider the code below :

if(char* thechar = new char[(strlen(pylon)+1)])
{
.....//do something
}
else
{
....//do something if fail to deploy memory
}

actually, "thechar" have been given a memory space in a if-scope, will
it destroy(delete) itself as soon as process goes out of the scope? or
it still need a delete to avoid memory leak?

tks all, regards.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Frederick Gotham
Guest





PostPosted: Mon Jul 03, 2006 1:38 am    Post subject: Re: will it cause memory leak? Reply with quote



Rayer posted:

Quote:
consider the code below :

if(char* thechar = new char[(strlen(pylon)+1)])
{
....//do something
}
else
{
...//do something if fail to deploy memory
}

actually, "thechar" have been given a memory space in a if-scope


"thechar" is a local automatic object (as opposed to a local static
object). Its scope is the body of the "if" statement, and any
corresponding else statements, as demonstrated by the following code:

int main()
{
if (char c = Cool
{
c = 7;
}
else if ( 1 )
{
c = 8;
}
else if ( 2 )
{
c = 78;
}
else
{
c = 72;
}


c = 45; /* Compiler ERROR */
}


Quote:
will it destroy(delete) itself as soon as process goes out of the
scope? or it still need a delete to avoid memory leak?


The type of "thechar" is char*. It is a pointer variable. All it does is
store an address.

In your particular code example, you have it store the address of a
memory chunk which was dynamically allocated.

If you dynamically allocate memory in C++, (be it by using "new", or be
it by using "malloc"), then it's *your* responsibility to free it. Memory
allocated by "new" is freed by passing the address on to "delete", as
follows:

#include <new>

int main()
{
if( char *p = new(std::nothrow) char[64] )
{
delete [] p;
}
}


As for the object known as "thechar": Like all local automatic objects,
it's destroyed automatically as soon as it goes out of scope.


--

Frederick Gotham

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Bo Persson
Guest





PostPosted: Mon Jul 03, 2006 1:39 am    Post subject: Re: will it cause memory leak? Reply with quote



"Rayer" <shivanna (AT) gmail (DOT) com> skrev i meddelandet
news:e87n56$aup$1 (AT) netnews (DOT) hinet.net...
Quote:
consider the code below :

if(char* thechar = new char[(strlen(pylon)+1)])
{
....//do something
}
else
{
...//do something if fail to deploy memory
}

actually, "thechar" have been given a memory space in a if-scope,
will
it destroy(delete) itself as soon as process goes out of the scope?
or
it still need a delete to avoid memory leak?


Anything that you new, you also have to delete (or in this case,
delete[] ).

What goes out of scope is the pointer, not what it points to.


Bo Persson



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Jack Klein
Guest





PostPosted: Mon Jul 03, 2006 4:42 am    Post subject: Re: will it cause memory leak? Reply with quote

On 2 Jul 2006 08:08:51 -0400, Rayer <shivanna (AT) gmail (DOT) com> wrote in
comp.lang.c++.moderated:

Quote:
consider the code below :

if(char* thechar = new char[(strlen(pylon)+1)])
{
....//do something
}
else
{
...//do something if fail to deploy memory
}

actually, "thechar" have been given a memory space in a if-scope, will
it destroy(delete) itself as soon as process goes out of the scope? or
it still need a delete to avoid memory leak?

tks all, regards.

The pointer object "thechar" will go out of scope when the enclosing
block ends, and it will be destroyed, but that will not release the
dynamically allocated memory. You need:

delete[] thechar;

....inside the block to avoid memory leak.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alan Johnson
Guest





PostPosted: Mon Jul 03, 2006 4:42 am    Post subject: Re: will it cause memory leak? Reply with quote

Rayer wrote:
Quote:
consider the code below :

if(char* thechar = new char[(strlen(pylon)+1)])
{
....//do something
}
else
{
...//do something if fail to deploy memory
}

actually, "thechar" have been given a memory space in a if-scope, will
it destroy(delete) itself as soon as process goes out of the scope? or
it still need a delete to avoid memory leak?

tks all, regards.

Your code appears to assume that new will return a NULL pointer if it
fails, which is not true in the above situation. When new fails (at
least the version of new you are using), it throws a std::bad_alloc
exception. Also, delete is not called when a pointer goes out of scope.
You need to explicitly delete the character array you've allocated.
That is:
delete [] thechar ;

However, you could avoid the issue completely by using something like:
std::string s = pylon ;

--
Alan Johnson

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Damian 'LegioN' Szuberski
Guest





PostPosted: Mon Jul 03, 2006 4:43 am    Post subject: Re: will it cause memory leak? Reply with quote

Rayer wrote:
Quote:
it still need a delete to avoid memory leak?
Only the pointer will be out of range (destroyed - it's on stack),

char[] stored on the heap still needs to be deleted.
Kind regards.

--
Damian Szuberski

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Carl Barron
Guest





PostPosted: Mon Jul 03, 2006 4:10 pm    Post subject: Re: will it cause memory leak? Reply with quote

Rayer <shivanna (AT) gmail (DOT) com> wrote:

Quote:
consider the code below :

if(char* thechar = new char[(strlen(pylon)+1)])
{
....//do something
}
else
{
...//do something if fail to deploy memory
}

actually, "thechar" have been given a memory space in a if-scope, will
it destroy(delete) itself as soon as process goes out of the scope? or
it still need a delete to avoid memory leak?

tks all, regards.

My advice is don't use new/delete operators directly in any high

level code if possible not to do so. A common idiom is RAII
Resource Allocation Is Initialization, allocate in a ctor and
deallocate in a dtor of a non copyable class.

template <clsss T>
class buffer_ptr
{
typedef typename boost::shared_ptr<T> smart_type;
smart_type ptr;
struct array_deleter
{
template <class U>
void operator () (U* u) {delete [] u;}
};
public:
explicit buffer_ptr(std::size_t n)
:ptr(new T[n],array_deleter()){}

T* get() const {return ptr.get();}
operator bool() {return ptr.operator bool();}
};
// ...

if(buffer_ptr<char> buffer(std::strlen(pylon)))
{
char * ptr = buffer.get();
// don't delete ptr
// use ptr
}
else
{
// failure code,
}

it is possible to write a buffer_ptr as a non copyable stack class
which just holds array news a pointer on constrution and array deletes
in the dtor. with the member functions get() and operator bool().


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
eden
Guest





PostPosted: Mon Jul 03, 2006 4:19 pm    Post subject: Re: will it cause memory leak? Reply with quote

Rayer wrote:
Quote:
[...]
actually, "thechar" have been given a memory space in a if-scope, will
it destroy(delete) itself as soon as process goes out of the scope? or
it still need a delete to avoid memory leak?


I know you are probably not asking this, but if you mean would the
memory remain in the system after the process finishes, then no, system
will free the memory. But, you will have memory leaks.

Goran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.