What is Dangling pointer ?

Since Objective-C is based upon C foundation, it is possible to use C-style functions. However Objective-C’s full power is only unlocked if you make full use of its object-oriented extensions.

Before  talking about Dangling pointers first we  have to know about pointers.

Pointers :

A variable within an application can be considered to consist of four components. These are its:

  • Name
  • Location — where it is stored in memory
  • Type — what kind of data it can store
  • Current value

Pointers are different from other normal variables which can store values, pointers are special variables that can hold the address of a variable. Since they store memory address of a variable, the pointers are very commonly said to “point to variables”. Lets try to understand the concept.


A normal variable ‘var’ has a memory address of 1001 and holds a value 50. Apointer variable has its own address 2047 but stores 1001, which is the address of the variable ‘var’.

You can consider the memory of the iPhone as being made up of a large pile of bytes, each stacked one on top of another. Each byte has a number, called an address, associated with it, just like houses have an associated street number.Below figure  represents several bytes of the iPhone’s memory, starting at address 924 and extending through address 940.

  • When you allocate a variable within your application, the compiler reserves an explicit amount of memory for it. For example, a statement such as int x = 45 will cause the compiler to reserve 4 bytes of memory to store the current value of x. This is represented in figure 1 by the 4 bytes starting at address 928.

    Obtaining the Address of a Variable

    Referring to the name of a variable within an expression will access or update its current value. By placing the address-of (&) operator in front of a variable name we are able to learn the address at which the variable is currently stored.

    A variable that is able to store the address of another variable is called a “pointer”. This is because the variable is said to point to the location of another value. The following code snippet demonstrates how we can use the address-of operator:

    int x = 45;
    int* y = &x;

    This code snippet declares an integer variable x that is initialized to the value 45. It also declares variable y with a data type of int*. The * at the end of the data type indicates a pointer and means we do not want to store an actual integer value but rather the memory address at which one can be found. This pointer is then initialized with the address of variable x using the address-of operator. If variable x had been stored at address 928 (as previously mentioned), we could graphically represent the result of executing this code snippet by updating the memory map to be similar to that in figure below.

Notice how the 4 bytes allocated to store variable y now store the number 928. When interpreted as an address, this indicates the location of variable x, as indicated by the arrow. The expression b>y = &x can be read as “place the address of variable x into variable y”. Following a pointer Once you have an address stored within a pointer variable, it is only natural to want to determine the value of whatever it points at. This operation is called dereferencing the pointer and is also achieved using the * symbol, as demonstrated below.

int x = 45;
int *y = &x;
NSLog(@"The value was %d", *y);

The statement on the last line will print out the message “The value was 45” since the * in front of variable y causes the compiler to follow the pointer and access the value it currently points at. Instead of reading the value it is also possible to replace it, as demonstrated below. Confusingly this also makes use of the * operator:

int x = 45;
int *y = &x;
*y = 92;

The statement on the last line stores the value 92 at the address located within variable y. Referring to Figure 2, you will see that variable y stores (or points to) address 928; hence, executing this statement actually updates the value of variable x, even though x is never explicitly referred to within the statement.

Dangling pointers :

Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory.

As the system may reallocate the previously freed memory to another process, if the original program then dereferences the (now) dangling pointer, unpredictable behavior may result, as the memory may now contain completely different data. This is especially the case if the program writes data to memory pointed by a dangling pointer, as silent corruption of unrelated data may result, leading to subtle bugs that can be extremely difficult to find, or cause segmentation faults.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s