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 my memory be deallocated?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
s
Guest





PostPosted: Wed Mar 03, 2004 9:52 pm    Post subject: Will my memory be deallocated? Reply with quote



Regarding the following pseudocode:

<pseudocode>

char *buffer = new char[10];
//put some values in buffer
if( some_condition )
{
char *temp = new char[10];
//put some values in temp
delete[] buffer;
buffer = temp;
temp = NULL;
}
//continue doing stuff with buffer

</pseudocode>

Question:
Is the "temp = NULL;" statement necessary? If it still points to what
it was originally set, will that memory be deallocated when I leave the
scope of that if block?

Thanks,
s
Back to top
John Harrison
Guest





PostPosted: Wed Mar 03, 2004 9:58 pm    Post subject: Re: Will my memory be deallocated? Reply with quote




"s" <youshouldbe (AT) home (DOT) with.yourself> wrote

Quote:
Regarding the following pseudocode:

pseudocode

char *buffer = new char[10];
//put some values in buffer
if( some_condition )
{
char *temp = new char[10];
//put some values in temp
delete[] buffer;
buffer = temp;
temp = NULL;
}
//continue doing stuff with buffer

/pseudocode

Question:
Is the "temp = NULL;" statement necessary?
If it still points to what
it was originally set, will that memory be deallocated when I leave the
scope of that if block?

No, definitely no. Memory is allocated with new[] is deallocated using
delete[], no other way.

Quote:

Thanks,
s

john



Back to top
Dave
Guest





PostPosted: Wed Mar 03, 2004 10:02 pm    Post subject: Re: Will my memory be deallocated? Reply with quote




"s" <youshouldbe (AT) home (DOT) with.yourself> wrote

Quote:
Regarding the following pseudocode:

pseudocode

char *buffer = new char[10];
//put some values in buffer
if( some_condition )
{
char *temp = new char[10];
//put some values in temp
delete[] buffer;
buffer = temp;
temp = NULL;
}
//continue doing stuff with buffer

/pseudocode

Question:
Is the "temp = NULL;" statement necessary? If it still points to what
it was originally set, will that memory be deallocated when I leave the
scope of that if block?

Thanks,
s

temp = NULL has nothing to do with deallocation. You need to specifically
delete [] the address allocated in line "char *temp = new char[10];".



Back to top
Mike Wahler
Guest





PostPosted: Wed Mar 03, 2004 10:03 pm    Post subject: Re: Will my memory be deallocated? Reply with quote


"s" <youshouldbe (AT) home (DOT) with.yourself> wrote

Quote:
Regarding the following pseudocode:

pseudocode

char *buffer = new char[10];
//put some values in buffer
if( some_condition )
{
char *temp = new char[10];
//put some values in temp
delete[] buffer;
buffer = temp;
temp = NULL;
}
//continue doing stuff with buffer

/pseudocode

Question:
Is the "temp = NULL;" statement necessary?

No. (However it might be useful for testing purposes).

Don't lose the value now in 'buffer', or you'll leak memory.

Quote:
If it still points to what
it was originally set,


Whether it does or not ...

Quote:
will that memory be deallocated when I leave the
scope of that if block?

No. You allocated it, you must free it. This is why you
need to retain the value orignally returned by 'new' (as
you've done above), whether in the original pointer or some
copy of it.

-Mike



Back to top
David Harmon
Guest





PostPosted: Wed Mar 03, 2004 11:35 pm    Post subject: Re: Will my memory be deallocated? Reply with quote

On Wed, 3 Mar 2004 21:58:16 -0000 in comp.lang.c++, "John Harrison"
<john_andronicus (AT) hotmail (DOT) com> was alleged to have written:
Quote:
If it still points to what
it was originally set, will that memory be deallocated when I leave the
scope of that if block?

No, definitely no. Memory is allocated with new[] is deallocated using
delete[], no other way.

But don't forget std::auto_ptr etc.


Back to top
John Harrison
Guest





PostPosted: Thu Mar 04, 2004 8:15 am    Post subject: Re: Will my memory be deallocated? Reply with quote


"David Harmon" <source (AT) netcom (DOT) com> wrote

Quote:
On Wed, 3 Mar 2004 21:58:16 -0000 in comp.lang.c++, "John Harrison"
[email]john_andronicus (AT) hotmail (DOT) com[/email]> was alleged to have written:
If it still points to what
it was originally set, will that memory be deallocated when I leave the
scope of that if block?

No, definitely no. Memory is allocated with new[] is deallocated using
delete[], no other way.

But don't forget std::auto_ptr etc.


Well no, but what does auto_ptr use to deallocate memory? Either something
non-standard, or it uses delete (not delete[] of course).

john



Back to top
Duane Hebert
Guest





PostPosted: Fri Mar 05, 2004 1:23 am    Post subject: Re: Will my memory be deallocated? Reply with quote


"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote

Quote:

"s" <youshouldbe (AT) home (DOT) with.yourself> wrote in message
news:404653B5.D118B513 (AT) home (DOT) with.yourself...
Regarding the following pseudocode:

pseudocode

char *buffer = new char[10];
//put some values in buffer
if( some_condition )
{
char *temp = new char[10];
//put some values in temp
delete[] buffer;
buffer = temp;
temp = NULL;
}
//continue doing stuff with buffer

/pseudocode

Question:
Is the "temp = NULL;" statement necessary?

No. (However it might be useful for testing purposes).

Or preventing possible delete from the wrong pointer.




Back to top
Mike Wahler
Guest





PostPosted: Fri Mar 05, 2004 7:19 pm    Post subject: Re: Will my memory be deallocated? Reply with quote


"Duane Hebert" <spoo (AT) flarn2 (DOT) com> wrote

Quote:

"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote


"s" <youshouldbe (AT) home (DOT) with.yourself> wrote in message
news:404653B5.D118B513 (AT) home (DOT) with.yourself...
Regarding the following pseudocode:

pseudocode

char *buffer = new char[10];
//put some values in buffer
if( some_condition )
{
char *temp = new char[10];
//put some values in temp
delete[] buffer;
buffer = temp;
temp = NULL;
}
//continue doing stuff with buffer

/pseudocode

Question:
Is the "temp = NULL;" statement necessary?

No. (However it might be useful for testing purposes).

Or preventing possible delete from the wrong pointer.

That's still a 'test' (delegated to operator 'delete').

But assigning NULL blindly for that only purpose imo
is poor practice. One could easily be doing such a
'safe' delete, but leaving something else 'undeleted'
which should have been, possibly having it 'bite' you
later (e.g. assigning a new value to a pointer that's
still pointing to allocated storage.)

-Mike



Back to top
Duane Hebert
Guest





PostPosted: Sat Mar 06, 2004 12:27 am    Post subject: Re: Will my memory be deallocated? Reply with quote


"Mike Wahler" <mkwahler (AT) mkwahler (DOT) net> wrote


Quote:
Or preventing possible delete from the wrong pointer.

That's still a 'test' (delegated to operator 'delete').

But assigning NULL blindly for that only purpose imo
is poor practice. One could easily be doing such a
'safe' delete, but leaving something else 'undeleted'
which should have been, possibly having it 'bite' you
later (e.g. assigning a new value to a pointer that's
still pointing to allocated storage.)

I don't disagree. I don't use this idiom. Just commenting
on a "possible" use.






Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) 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.