| View previous topic :: View next topic |
| Author |
Message |
s Guest
|
Posted: Wed Mar 03, 2004 9:52 pm Post subject: Will my memory be deallocated? |
|
|
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
|
Posted: Wed Mar 03, 2004 9:58 pm Post subject: Re: Will my memory be deallocated? |
|
|
"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.
john
|
|
| Back to top |
|
 |
Dave Guest
|
Posted: Wed Mar 03, 2004 10:02 pm Post subject: Re: Will my memory be deallocated? |
|
|
"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
|
Posted: Wed Mar 03, 2004 10:03 pm Post subject: Re: Will my memory be deallocated? |
|
|
"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
|
Posted: Wed Mar 03, 2004 11:35 pm Post subject: Re: Will my memory be deallocated? |
|
|
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
|
Posted: Thu Mar 04, 2004 8:15 am Post subject: Re: Will my memory be deallocated? |
|
|
"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
|
Posted: Fri Mar 05, 2004 1:23 am Post subject: Re: Will my memory be deallocated? |
|
|
"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
|
Posted: Fri Mar 05, 2004 7:19 pm Post subject: Re: Will my memory be deallocated? |
|
|
"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
|
Posted: Sat Mar 06, 2004 12:27 am Post subject: Re: Will my memory be deallocated? |
|
|
"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 |
|
 |
|