Debugging Memory Problems with MemoryScape : Chapter 1 Locating Memory Problems : Fixing Dangling Pointer Problems : Dangling Pointers

Dangling Pointers
If you enable memory debugging, TotalView displays information in the Variable Window about the variable’s memory status; that is, whether the memory is allocated or deallocated. The following small program allocates a memory block, sets a pointer to the middle of the block, and then deallocates the block:
main(int argc, char **argv)
{
int *addr = 0; /* Pointer to start of block. */
int *misaddr = 0; /* Pointer to interior of block. */
addr = (int *) malloc (10 * sizeof(int));
misaddr = addr + 5; /* Point to block interior */
/* Deallocate the block. addr and */
/* misaddr are now dangling. */
free (addr);
}
Figure 25 shows two Variable Windows. Execution was stopped before your program executed the free() function. Both windows contain a memory indicator saying that blocks are allocated.
 
After your program executes the free() function, the messages change, Figure 26.
 

Rogue Wave Software, Inc.
Voice: (303) 473-9118
rwonlinedocs@roguewave.com