Reputation:
“Because pnValue is the only variable holding the address of the dynamically allocated integer, when pnValue is destroyed there are no more references to the dynamically allocated memory. This is called a memory leak. As a result, the dynamically allocated integer can not be deleted, and thus can not be reallocated or reused.” {quoted from LearnCpp.}
Why does pnValue holds the address of the dynamically allocated integer? If pointers just point to the address, why should destroying the pointer affect the address? Does this mean that in dynamically allocated integers, we must always have pointers because the pointer somehow is the address?
Upvotes: 2
Views: 2655
Reputation: 88155
Why does pnValue holds the address of the dynamically allocated integer? If pointers just point to the address, why should destroying the pointer affect the address?
You're getting confused by two different uses of the term 'address.' The word can be used to refer both to information about how to locate a place and it can be used to refer to the place itself. Consider:
1) I've written the address on a piece of paper.
2) I'm driving to the address now.
Using the first sense, pointers store an address; information on how to locate a particular piece of data or memory. Using the other sense, pointers point at an address; they point at the location of the data or memory. Just like if you write an address (information on getting to a place) on a piece of paper, that piece of paper now points to the address (the place).
So destroying the pointer destroys the information on how to access the location.
Does this mean that in dynamically allocated integers, we must always have pointers because the pointer somehow is the address?
When you allocate memory using new
, you are given information on how to access the allocated memory. A leak is what happens if you fail to eventually give that same information back to delete
. So yes, you must always somehow maintain that information so that you can eventually give it back to delete
.
Upvotes: 1
Reputation: 11
Like Benjamin said, Pointer store the Address of the Data you want to access. If you create a pointer you also have to reserve how much memory you want. Otherwise you can easily create access violation. You should also be aware that memory leaks are not just annoying because the vitrual space of your application increases, but wrong handling with pointer is also a good entry point for everyone who wants to break in you application or your system. For finding memory leaks there are some projects @codeproject.com like this one here: http://www.codeproject.com/Articles/9815/Visual-Leak-Detector-Enhanced-Memory-Leak-Detectio
Upvotes: 1
Reputation: 63707
All objects that have been defined exist in memory and has an address. An address of the object is the location of the memory from where, the object starts residing. So in a one dimensional memory space, if an object starts residing at the slot 100, then 100 is said to be the address of the object.
99 100 101 102 103 104 105 106
=====================================
| O | B | J | E | C | T |
=====================================
^
|
|
pnObject
Pointers are objects which contains addresses of other objects. When you create an object dynamically, it is created in the heap memory, and has no scope. It continues to exist as long as you don;t delete the allocated memory explicitly or the program allocating the memory dies.
Memory is allocated dynamically through the c-library function malloc
or the c++
operator new
, which returns the address, the starting location in the heap memory from where the object starts residing. You are supposed to save the address in a variable ( a particular type called a pointer), which now holds the address of the object.
Object *pnValue = new Object();
As long as you know the address of the newly created object in heap, you have control but if you forget the address, either by destroying the pointer variable, overlaying the content or simply not storing the address, you cannot access that location and going forward cannot free/delete the allocated memory.
99 100 101 102 103 104 105 106
=====================================
| O | B | J | E | C | T |
=====================================
Upvotes: 5
Reputation: 103693
Pointers don't point to addresses, they store addresses. They are said to "point to" objects. The object they point to is the object located in the address which they store.
Think of an address book. Each entry is a pointer to one of your friends (your friends are objects). Assume your friends are spread across the country (your computer's memory), and your memory is crap. So you need your address book to find your friends. If you lose a page of your address book (the pointer is destroyed), the friends whose addresses were on that page are lost to you. That is, unless you had a back up copy (more pointers holding the same addresses).
Upvotes: 2
Reputation: 564333
Pointers "just point to the address". What this is saying is, if you remove all pointers which point to that address, there's no way to "reach" the memory stored there in order to delete/free it. As soon as no pointer "points" to that location, there's no way for you to clean up that memory any more, so you get a leak.
Destroying the pointer does not create the leak - if there is another pointer which you can use to reach the memory in order to clean it up. Destroying the pointer just removes the ability to reach that memory if no other pointers are kept.
Upvotes: 4