Dangling Pointers & Memory Leaks

Dangling pointer

Pointer points to a memory location that no longer exists.

Memory Leaks

Memory allocated by a program is not freed.

The program is particularly acute if memory allocated in heap can no longer be accessed.

void foo(){
    int * p = malloc(8);
    p = NULL;
    /* memory previously pointed by p can never
     * be freed. */
}
int * f2(int null){
    int mem2[num];
    return mem2;
} 
// DANGLING POINTER
// never return the address of a local variable.
Published on October 16, 2015