whats the result of this ...
-
Hi all I have piece of code like this ... char* str; str= new char[30]; str = new char[20]; .... delete str; Here what will happen ? To try it , with VS2003 I did not face any memory leaks (Or did I fail to notice it ?) My question is : When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ? Will "new" make sure the first 30 bytes are freed ? I noticed one thing . After the first "new" the location of str was 0x60878776 . After the second "new" the str was pointing to 20 bytes starting from 0x83422323 . Does it mean memory of 0x60878776 is free ?
redindian
-
Hi all I have piece of code like this ... char* str; str= new char[30]; str = new char[20]; .... delete str; Here what will happen ? To try it , with VS2003 I did not face any memory leaks (Or did I fail to notice it ?) My question is : When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ? Will "new" make sure the first 30 bytes are freed ? I noticed one thing . After the first "new" the location of str was 0x60878776 . After the second "new" the str was pointing to 20 bytes starting from 0x83422323 . Does it mean memory of 0x60878776 is free ?
redindian
Let me check where the data is... But before that, I have to say that your syntax is wrong. It should be:
char* str; str= new char[30]; str = new char[20]; // .... delete **[]** str;
Maxwell Chen
-
Let me check where the data is... But before that, I have to say that your syntax is wrong. It should be:
char* str; str= new char[30]; str = new char[20]; // .... delete **[]** str;
Maxwell Chen
Visual C++ 2005 reuses the first address of
str
where it was allocated.
Maxwell Chen
-
Hi all I have piece of code like this ... char* str; str= new char[30]; str = new char[20]; .... delete str; Here what will happen ? To try it , with VS2003 I did not face any memory leaks (Or did I fail to notice it ?) My question is : When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ? Will "new" make sure the first 30 bytes are freed ? I noticed one thing . After the first "new" the location of str was 0x60878776 . After the second "new" the str was pointing to 20 bytes starting from 0x83422323 . Does it mean memory of 0x60878776 is free ?
redindian
dharani wrote:
When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ?
The allocated memeory will become inaccessible (and thus un-deletable), because you do no longer have a 'handle' to it.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words -
Visual C++ 2005 reuses the first address of
str
where it was allocated.
Maxwell Chen
-
dharani wrote:
When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ?
The allocated memeory will become inaccessible (and thus un-deletable), because you do no longer have a 'handle' to it.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words -
dharani wrote:
So how to avoid such a scenario ?
delete[]
the old memoryadress before reallocating. Hold the new address in another variable. Use a string class (e.g.std::string
orCString
), which handles the memory for you.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words -
Use
realloc
. Regards, Paresh. -
Use
realloc
. Regards, Paresh. -
You shoudln't mix new and realloc.
"What's on your mind, if you will allow the overstatement?"
Yes, you are right. I was thinking in malloc context. Regards, Paresh.
-
So you mean to say in VS2003 or VC++ 6 the first memory block will be still occupied ?
redindian
i would have thought so as you haven't freed it explicitly, or your compiler has done it for you, why don't you put cout << "destructor\n"; in your destructor and see? paul
if ignorance is bliss then knock the smile off my face!!!
-
You shoudln't mix new and realloc.
"What's on your mind, if you will allow the overstatement?"
swathee wrote:
You shoudln't mix new and realloc.
offcouse..
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief
-
dharani wrote:
I am trying to find answer ...
CString is much better optimized in this case!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief