943,691 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 547
  • C++ RSS
Feb 17th, 2009
0

side-effect of the point assignment

Expand Post »
Hi, all
i am reading some memory leaking problem articles now.
Here is one question i can't understand:
c++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. delete []b;
used for??
And why the last line of the code free the dynamic variable associated with pointer a??

Thanks.
Reputation Points: 10
Solved Threads: 2
Junior Poster
shasha821110 is offline Offline
123 posts
since Jan 2009
Feb 17th, 2009
1

Re: side-effect of the point assignment

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.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Feb 18th, 2009
1

Re: side-effect of the point assignment

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.
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Feb 19th, 2009
0

Re: side-effect of the point assignment

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".
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Feb 19th, 2009
0

Re: side-effect of the point assignment

I was thinking maybe we should throw one thread about memory leak on the DANIWEB.
Its really important for beginners.
Reputation Points: 10
Solved Threads: 2
Junior Poster
shasha821110 is offline Offline
123 posts
since Jan 2009
Feb 19th, 2009
0

Re: side-effect of the point assignment

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: 'Shape' not declared
Next Thread in C++ Forum Timeline: Help with Classes !!! (Homework)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC