Thread: Dangling Pointers in C

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    Dangling Pointers in C

    By the defination of Dangling Pointers I don't understand what does exectly it mean in c language

    Can anyone tell me what is dangling pointer in my code ?

    Code:
    #include <stdlib.h>#include <stdio.h>
    
    
    int main()
    {
    	int *ptr = malloc(sizeof(*ptr));
    	 if( ptr == NULL )                      /*Check for failure. */
         {
           puts( "Memory not allocated" );
           exit( 0 );
         }
        
    	puts( "Allocated memory!" );
    	
    	free(ptr); /*Release memory. */
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    In one sense, you don't have a dangling pointer because you free all the memory you allocated.

    In another sense, you still have a non-null pointer pointing to some area of memory.
    Code:
        free(ptr); /*Release memory. */
        *ptr = 42;  /*This is bad, and is called "use after free" */
    Use after free errors are the subject of many vulnerability issues.
    Just type in "use after free" to CWE - Search the CWE Web Site to see all the fun.

    Tools like 'valgrind' can certainly help to locate such issues.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Quote Originally Posted by Salem View Post
    In one sense, you don't have a dangling pointer because you free all the memory you allocated.
    Does ptr become dangling pointer when we free memory ?

    Code:
      free(ptr);
    When assigning a null value to it, it is no longer a dangling pointer.
    Code:
     ptr=NULL;/* ptr is no longer dangling */

  4. #4
    Registered User
    Join Date
    Feb 2022
    Posts
    45
    Pretty much that, yes. The main issue with a dangling pointer is that it is one pointing to unallocated memory. When a pointer is declared, if it isn't initialized, it may be dangling because you don't know where it is pointing; but a pointer may also dangle if it was initialized to allocated memory but then the memory is freed. When you then set the pointer to NULL, it is no longer dangling.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Schol-R-LEA-2 View Post
    Pretty much that, yes. The main issue with a dangling pointer is that it is one pointing to unallocated memory. When a pointer is declared, if it isn't initialized, it may be dangling because you don't know where it is pointing; but a pointer may also dangle if it was initialized to allocated memory but then the memory is freed. When you then set the pointer to NULL, it is no longer dangling.
    Actually if you don't NULL a pointer after freeing the memory and then later attempt to access the memory, you don't know if the memory is in use by some other part of the program. Bottom line, always initialize all pointers to NULL when defined, always check to see if the allocation was successful, and always NULL the pointer immediately after calling free()!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Preventing Dangling Pointers
    By Asymptotic in forum C Programming
    Replies: 2
    Last Post: 09-04-2017, 01:17 PM
  2. Question about Stray or Dangling Pointers
    By Jonathan Yaniv in forum C++ Programming
    Replies: 13
    Last Post: 06-12-2012, 11:27 PM
  3. Pointer dangling or not
    By vaibhav in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2006, 06:39 PM
  4. dangling pointers
    By lydiapeter in forum C Programming
    Replies: 5
    Last Post: 08-31-2005, 03:49 AM
  5. Dangling Else error?
    By OttoDestruct in forum C++ Programming
    Replies: 9
    Last Post: 09-01-2004, 06:25 PM

Tags for this Thread