Dangling Pointers in C programming

Dangling Pointers in C Programming – Be Strictly Avoided

Table of Contents
    Add a header to begin generating the table of contents

    Dangling Pointers : Be “STRICTLY” Avoided !

    Pointers are like sharp knives in C-Programming which are having tremendous power. Pointers, when used properly reduces the complexity of the programs to a great extent.

     

    But, on the counterpart when not used properly results in adverse effects, in an worst case, it may crash the system. In this blog, I am mainly focusing on two different situations where Dangling pointers in C programming be avoid it in a simple way.

    • Note 1:

      code snippets provided here is tested with GCC compiler [ gcc version 4.7.3 ] running under Linux environment.

    • Dangling pointers in C programming :Simply, the pointers not pointing to the valid memory location ( Object ).To Explain with, I am considering the two different cases , giving rise to Dangling Pointers.

    • Case :1. When a function returns address of the auto-variables.

    • Case :2. In dynamic memory allocation, using the block of memory which is already free

    Case 1: Function Returning Address of the Auto-variables.

    Consider the simple code given below in fig 1,

    Dangling Pointers in C programming
    Fig 1: Code returning the address of the local / auto variables.

    When we run the above program in gcc compiler we get the warning as given in the Fig 2,

    Dangling Pointers in C programming
    Fig 2: Compiler generating warning.

    But, What is wrong when address of the auto/local variables are returned ? Let us see this,

    • We know that whenever , there is a function call, new stack frame will be created automatically.
    • When the control returns from the function call, all the memory allocated for that function will be freed automatically.
    • In our example program, we are returning the address of the auto variable and collecting this in the ‘int_ptr ‘pointer in the main function. So, int_ptr is still pointing to the memory which is freed as per the point no.2 .
    • One can observe allocation & de-allocation of stack frame(Let us called this as fun frame) for the fun() in the Fig 3 given below,
    • Now, int_ptr becomes ‘Dangling’. De-referencing this pointer results in ‘Unexpected output’
    • So, always take extra care while playing with pointers & local variables.

    Case 2: In Dynamic Memory Allocation, Using the Block of Memory Which Is Already Freed.

    Consider the below sample code,

    Dangling Pointers in C programming
    Fig 3: Code snippet, showing re-using of block_ptr.

    Explanation: For the code snippet shown in Fig 4

    • Line 5:Memory allocation by malloc().
    • Line 9:Memory allocated is freed by free() manually.
    • Note 2: 
    • In case 1: Memory is freed automatically.
    • In case 2: Memory is freed manually. This is the one of key difference between stack & heap.
    • Line 13:Re-using the pointer, which is still pointing to the memory which is already freed. In our example block_ptr is now called as “Dangling Pointer” 
    • But, In case 2 pointer becoming dangling can be avoided by initializing it to ‘NULL’ immediately after freeing it. 

    Dangling pointers are very harmful and results in adverse effects in the embedded systems programming. So. It should be “STRICTLY” avoided

    Serial No Related Blog Posts Links
    1. Difference between Embedded System programming and IoT programming Click Here
    2. Our Training Programs for Freshers Click Here
    3. Emertxe Placements in Embedded Systems and Embedded IoT – 2022 (Jan-sept) Report Click Here

    People Also Ask(PAA)

    Dangling pointers in C programming pose risks like unpredictable behavior, memory access violations, memory leaks, data integrity issues, and difficult debugging. Avoid them by practicing proper memory management

    • Initialize pointers and set them to NULL.
    • Manage memory properly using malloc() and free().
    • Be mindful of variable scope and lifetime.
    • Debug and test your code.   

    By following these steps, you can prevent dangling pointers and ensure effective memory management in your C programs.

    Share this material with your friend: