473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Will my memory be deallocated?

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
Jul 22 '05 #1
8 1601

"s" <yo*********@home.with.yourself> wrote in message
news:40***************@home.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?
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
Jul 22 '05 #2

"s" <yo*********@home.with.yourself> wrote in message
news:40***************@home.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? 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];".
Jul 22 '05 #3

"s" <yo*********@home.with.yourself> wrote in message
news:40***************@home.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).

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
Jul 22 '05 #4
On Wed, 3 Mar 2004 21:58:16 -0000 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> 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.

Jul 22 '05 #5

"David Harmon" <so****@netcom.com> wrote in message
news:40****************@news.west.earthlink.net...
On Wed, 3 Mar 2004 21:58:16 -0000 in comp.lang.c++, "John Harrison"
<jo*************@hotmail.com> 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
Jul 22 '05 #6

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:MC******************@newsread2.news.pas.earth link.net...

"s" <yo*********@home.with.yourself> wrote in message
news:40***************@home.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.

Jul 22 '05 #7

"Duane Hebert" <sp**@flarn2.com> wrote in message
news:nC*******************@weber.videotron.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message

news:MC******************@newsread2.news.pas.earth link.net...

"s" <yo*********@home.with.yourself> wrote in message
news:40***************@home.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
Jul 22 '05 #8

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:Qo42c.22928
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.

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: binaya | last post by:
Hi all, what actually goes wrong when I deallocated the memory twice ..? say I have int *p; p=new(int); now i run the command;
2
by: E G | last post by:
Hi, I have a class similar to this: class Matrix { private: float **_A; unsigned _rows,_cols; public:
18
by: Jan | last post by:
Hi there, i've got an STL map with something like this ( map<string, Object*> xyz; ) What happens when I call xyz.clear()? Is only the map cleared or the map and the Objects, so that the memory...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
14
by: madhawi | last post by:
i want to know that on what situation memory leakage happan and what is the solution to solve the problem of memory leakage.
16
by: KG | last post by:
Hi, I do have a question. int main() { char *p = (char *)malloc(9); strcpy(p,"TajMahal"); p++;
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
25
by: Andreas Eibach | last post by:
Hi again, one of the other big woes I'm having... typedef struct perBlockStru /* (structure) Long words per block */ { unsigned long *lword; } lwperBlockStru_t;
0
by: bieffe62 | last post by:
On 21 Ott, 17:19, Rolf Wester <rolf.wes...@ilt.fraunhofer.dewrote: To be sure that the deallocated memory is not cached at some level to be reused, you could try someting like this: while...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.