side-effect of the point assignment

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 123
Reputation: shasha821110 is an unknown quantity at this point 
Solved Threads: 2
shasha821110 shasha821110 is offline Offline
Junior Poster

side-effect of the point assignment

 
0
  #1
Feb 17th, 2009
Hi, all
i am reading some memory leaking problem articles now.
Here is one question i can't understand:
  1. char *a= new char[10];
  2. char *b= new char[10];
  3. b=a;//pointer assignment
  4. delete []b;

In this article, it mentioned that the pointer assignment have side-effect and we couldn't delete pointer b from heap anymore.
its better use strcpy(b,a) function.

My question is:
is that correct??
if we couldn't delete b from heap what's the
  1. delete []b;
used for??
And why the last line of the code free the dynamic variable associated with pointer a??

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: side-effect of the point assignment

 
1
  #2
Feb 17th, 2009
It's saying that you can't delete the original "b" anymore because you've lost it's address by assigning a to b, which overwrites the previous address that b was holding. So the last line frees the original a, since b now points to a.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: side-effect of the point assignment

 
1
  #3
Feb 18th, 2009
Since you assign the value in "a" to "b" you effectively lose the address of the char [] pointed to by "b." You can delete the value pointed to by "b," which will be the same char [] pointed to by "a," but you cannot access the other char [] since you have no pointer to it.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: side-effect of the point assignment

 
0
  #4
Feb 19th, 2009
The above two posts have already informed you about the "side-effect" of the pointer assignment. I just want to add to it that would throw some light on the memory leak part.
When you permanently loose the base address of the memory chunk pointed to by b, you also loose your chance to free that memory, since to free any memory in langauges such as C/C++ you would absolutely need the pointer to it. Now, since you cannot free this memory and since it cannot be reclaimed by the language on it's own (like langauges that do offer such mechanism - Java) this chunk of memory is permamnently lost from the available pool of memory for your program. This memory has "leaked/drained" from the memory pool, and hence this is called memory leak. Too many such "leaks" would ultimately cause the pool to get exhausted and what you would get is an introduction to the famous "segmentation fault".
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 123
Reputation: shasha821110 is an unknown quantity at this point 
Solved Threads: 2
shasha821110 shasha821110 is offline Offline
Junior Poster

Re: side-effect of the point assignment

 
0
  #5
Feb 19th, 2009
I was thinking maybe we should throw one thread about memory leak on the DANIWEB.
Its really important for beginners.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: side-effect of the point assignment

 
0
  #6
Feb 19th, 2009
Why not you can go ahead and start one. I am sure there are enough C/C++ "gurus" who might be able to throw much more light on that subject and also bring out a few things that many of us still don't know.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC