Prev | Next |
The memory | ||||||
---|---|---|---|---|---|---|
The memory is a large array of bytes, numbered 0,1,2,... up to some large number. Each byte holds a number from -128 to 127. Byte number 1 has address 1, byte number 2 has address 2, etc. Each thing that you store in the memory occupies some number of bytes. An ASCII character occupies one byte. An integer typically occupies four bytes. An array might occupy 1000 bytes. You refer to a collection of bytes by the address of the first byte in the collection. A program breaks its memory into a few areas, including the following.
|
Pointers | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A pointer is a memory address. So, in a sense, a pointer is an integer. On a 32 bit machine, a pointer occupies four bytes. Pointers are not treated quite like integers in C/C++. Instead, a specialized set of operations is provided for them. What you can do with a pointer is
Each pointer has a type. If T is a type, the type T* is the type of a pointer that points to something of type T. For example, type int* is the type of a pointer to an int.
|
Pointer variables and constants | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Allocating memory in the heap | ||||||
---|---|---|---|---|---|---|
The usual way to get a pointer is not to get the address of a variable that is in the run-time stack, but instead to get the address of some memory in the heap. To get memory from the heap, you can use either C or C++ style. C++ style is the most pleasant by far.
|
Pointer diagrams |
---|
To use pointers, you will need to do careful hand simulations. You usually draw a pointer variable as a box that contains one end of an arrow. The arrow points to another box, representing the memory whose address is in the pointer variable. For example, after performing int* q = new int; int* r = q; *q = 50;you will have the following diagram. -- ---- q| -|------->| 50 | -- ---- ^ -- | r| -|---------- --Always draw careful pointer diagrams when using pointers! Sloppiness, or failure to draw diagrams at all, will result in programs that do not work. |
Freeing memory |
---|
The malloc function and new operator make use of memory managers that keep track of available memory. They might use the same memory manager, but might use different ones. When you are done with memory that you allocated in the heap using new or malloc, you can return it to the memory manager so that it can be used again later. Returning memory is called freeing the memory or deallocating it. Suppose that pointer p points to memory that you have allocated. If you allocated that memory using new, you write delete p;to return that memory to the system. If you allocate that memory using malloc, use free(p);It is important only to return something to the memory manager that was allocated by that memory manager. The rule is
Be very careful about deallocating memory. Keep in mind that you can have more than one variable that holds a pointer to a given piece of memory. If you deallocate the memory, all of those pointers become stale. They continue to hold memory addresses, but the memory to which they refer does not belong to you any more. You have no idea what that memory will be used for after it has been deallocated. A pointer that points to deallocated memory is called a dangling pointer. Problems that occur from the use of dangling pointers are among the most devastating, and most difficult to eliminate, of all the problems that you can have. By avoiding them with care, you will not be subjected to the pain of finding them by laborious debugging. If you fail to deallocate memory that you have allocated, and you let that memory become inaccessible to you, the system will not take that memory back automatically. The system will presume that your program still needs the memory. The result is a memory leak. Memory leaks are quite benign for programs that do not use much memory, but can be deadly in long runs, when your program will run out of memory. |
Prev | Next |