UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

6.10.5.  Accessing memory with a corrupt pointer

When dynamic memory is reserved, the systems marks the portion as occupied and the data is kept in that location. However, when the memory is deallocated, its content is no longer guaranteed, and it might be used by the operating system for internal purposes.

This observation is important because the free function receives a pointer as only parameter, deallocates its content, but there is nothing preventing another access with that pointer in what is known as a corrupted pointer access. The following code portion shows an example of this problem.

struct list_element 
{
  int;
  struct list_element *next;
}; 

void destroy(struct list_element *l) 
{
    while (l != NULL) 
    {
      free(l);
      l = l->next;
    }
    return;
}

The line that advances through the chain of pointers l = l ->next is accessing the memory portion pointed to by l which has been previously deallocated, thus its content is not guaranteed and the field next might not contain the expected data. One way to solve this problem is to copy the pointer to a location that without this problem, for example, a local variable.