| | |
side-effect of the point assignment
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 123
Reputation:
Solved Threads: 2
Hi, all
i am reading some memory leaking problem articles now.
Here is one question i can't understand:
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
used for??
And why the last line of the code free the dynamic variable associated with pointer a??
Thanks.
i am reading some memory leaking problem articles now.
Here is one question i can't understand:
c++ Syntax (Toggle Plain Text)
char *a= new char[10]; char *b= new char[10]; b=a;//pointer assignment 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)
delete []b;
And why the last line of the code free the dynamic variable associated with pointer a??
Thanks.
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".
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 !!!
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 !!!
![]() |
Other Threads in the C++ Forum
- Previous Thread: 'Shape' not declared
- Next Thread: Help with Classes !!! (Homework)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





