A running program is called a process. It is useful to think of each block of memory as having one of a few possible ownerships.
The operating system (OS) keeps track of memory that a given process owns. The OS does not allow a process to look at or store anything into memory that it does not own.
The heap manager is part of a process. It asks for memory from the OS as needed, and can return memory to the OS. Typically, it either requests or gives back fairly large chunks of memory.
The heap manager keeps its own table concerning the memory in the heap. Some of that memory has been allocated by the heap manager to the program, and the program is free to do whatever it chooses with that memory. Other parts of the heap are marked as belonging to the heap manager. It still belongs to the process, but should not be used by the program. The OS does not know about the heap manager's concept of ownership of memory in the heap. As far as the OS knows, it is all owned by the process.
Some of a process's memory is used for the run-time stack. The OS keeps track of the boundary of the run-time stack, and does not allow a program to use memory that has not been allocated as part of a frame. So memory in the run-time stack is allocated and deallocated from the OS in small chunks that correspond to frames.
A memory fault occurs when a process either uses memory in an incorrect way or uses memory that does not belong to it, according to the OS. On a Linux system, a memory fault is called a segmentation fault. When a memory fault occurs, the OS terminates the process immediately.
A dangling pointer
is a pointer to memory that your
program should not use. There are a few forms that a dangling
pointer can have.
that some memory is marked read-only. If your program
tries to change something in read-only memory, it will get a memory
fault. For example, a string constant is stored in read-only
memory. We will see that string constants can be treated as
char* str = "a string constant"; str = 'b';leads to a memory fault, since it tries to alter the memory where "a string constant" is stored. Do not use a string constant as a substitute for allocating memory using new.
Memory faults due to null pointers
|Memory address 0 is never owned by your process. Any attempt to use the memory at address 0 will lead to a memory fault.|
Memory faults due to running out of memory
|If your program runs out of memory, either in the run-time stack or the heap, it will probably get a memory fault.|
Using a dangling pointer can cause very strange errors. If something happens that appears to be impossible, then you should suspect that you are using a dangling pointer.
If you get a memory fault, use a debugger to track down exactly where the fault occurs. Just run the program in the debugger. When the memory fault occurs, the program will stop. Look at the values of pointers and array indices at that point. If you are not sure whether a pointer p is valid, try to show the value of *p.
In a Linux system, a null pointer is shown as 0x0 or (null); a pointer into the heap is a relatively small number such as 0x6a003c; and a pointer into the run-time stack is a very large number, such as 0xffffffffffff2104.
What is a dangling pointer? Answer
What is a memory fault? Answer
If you delete memory and immediately try to use it, do you always get a memory fault? Answer
Look at the following.
int* p = new int; int* q = p; *p = 50; *q = 100; delete p; printf("*q = %i\n", *q); delete q;Comment on what happens when this is run. Answer
int* p; *p = 50; printf("*p = %i\n", *p);Comment on what happens when this is run. Answer
What are the consequences of using a dangling pointer? Answer