TeachingBee

3 Cases That Can Cause Dangling Pointer In C

dangling pointer in c

Pointers in C are very powerful tool that can reduce the complexity of code to great level if used properly. If pointers are not handled properly it can even crash the system. One such type of mishandled pointers is dangling pointer in C.

Dangling Pointer in C programming language are those pointers that don’t point to any valid object. In other words a pointer pointing to a memory location that has been deleted is called dangling pointer in C.

In this article we will focus on scenarios which can cause dangling pointers and also ways how to avoid them.

Dangling Pointer in C
Dangling Pointer in C

Different Cases That Can Cause Dangling Pointer In C

Three different ways where Pointer acts as dangling pointer in C are:

Scenario 1: Function Call Returning Auto Variable Address

// The pointer pointing to local variable becomes
// dangling when local variable is not static.

#include <stdio.h>

int * fun(void) {

  // Variable var goes out of scope after function execution
  int
  var = 10;
  return ( &
    var);
}

int main() {
  // pointer p points to  address which is already freed.
  int * p = fun();
  printf(“The value at address % p: % d\ n”, p, * p);
  return 0;
}

Consider above program the variable var has limited scope within function and on each function call new stack is created. And when the control returns back from function to main function stack is freed automatically.

But in the program the address of variable is returned and getting stored in pointer p which is already freed when controls return back to main function.Thus pointer p points to memory which is already freed and hence becomes dangling pointer in C.

This causes other issues as well, like when again function call is made or the pointed memory address is allocated to some other variable or process, it can lead to incorrect behaviour and information leak as well.

Prevention

The above problem can be solved if variables scope is increased beyond the function like declaring to static will make its scope throughout the program.

Scenario 2: Variable goes out of scope 

In the below program the pointer p is assigned address of variable x which has limited scope within the block. When code comes out of the block the pointer still points to the address of x which is automatically freed as control comes out of the block, hence making pointer dangling.

// The pointer becomes dangling if it points to variable which has became out of scope.

#include <stdio.h>

int main() {
  int * p;
  //Block started
  {
    int x = 1000;
    p = & x;
  }
  //After this block p is dangling
  return 0;
}

Prevention

The following case of dangling pointer in C can be prevented by extending the scope of variable x outside the block.In that case memory will not be freed as controls come out of block. Or in other case limited the scope of pointer to the block itself will solve the problem.

Scenario 3: Memory De-allocation

// Reusing the pointer after freeing it, makes pointer dangling.

#include <stdlib.h>

int main() {
  int * p = (int * ) malloc(sizeof(int));
  //Do something with allocated memory

  free(p);

  // Dangling pointer
  * p = 20;
  return 0;
}

In the code above memory allocation is done dynamically using malloc() and memory is freed using free() manually. On reusing the same pointer which is still pointing to freed memory, makes pointer p dangling

Prevention

This case of dangling pointer in C can be avoided by initialising it to NULL immediately after freeing it as shown below:

// Initialising the pointer to NULL makes pointer no more dangling

#include <stdio.h>
#include <stdlib.h>

int main() {

  // Memory Allocation
  int * p = (int * ) malloc(sizeof(int))

  // Memory freed
  free(p);

  //Initialising the pointer to NULL
  // p pointer is no more dangling
  p = NULL;

  return 0;
}

Conclusion

Dangling pointer In C can have adverse effects in embedded systems programming, hence should be strictly avoided. In this article we discussed about dangling pointers and cases where they can arise and also saw how we can prevent them

Got a question or just want to chat? Drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Subtraction of Two Numbers Program in C

In this blog post, we will checkout the Subtraction of Two Numbers Program in C. How to Write Subtraction Program in C? To create a C program to subtract two

Add Two Numbers In C

In this blog post, we will checkout the how to add two numbers in C. Algorithm to Add Two Numbers in C To create a C program to add two

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In