Will my memory be deallocated?

S

s

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
 
J

John Harrison

s said:
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.
Thanks,
s

john
 
D

Dave

s said:
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];".
 
M

Mike Wahler

s said:
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.
If it still points to what
it was originally set,


Whether it does or not ...
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
 
D

David Harmon

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.
 
J

John Harrison

David Harmon said:
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
 
D

Duane Hebert

Mike Wahler said:
s said:
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.
 
M

Mike Wahler

Duane Hebert said:
s said:
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
 
D

Duane Hebert

Mike Wahler said:
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.
 

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

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top