Jump to content
Korean Random

Dangling Pointer in C?


Recommended Posts

Hello Everyone, I am preparing for my upcoming interview which is c language based. I want to know concept of dangling pointer in C? and In how many ways can a pointer act as a Dangling Pointer? When A pointer is pointing to a memory location, and a B pointer deletes the memory occupied by the A pointer, then the A pointer still points to the memory location? So Can anyone know about it and explains to me with some examples of dangling pointer.

Link to comment
Short link
Share on other sites

Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory. In short pointer pointing to non-existing memory location is called dangling pointer. 

Example:

 

#include<stdlib.h>
{
    char *ptr = malloc(Constant_Value);
    .......
    .......
    .......
    free (ptr);      /* ptr now becomes a dangling pointer */
}
Link to comment
Short link
Share on other sites

  • 2 years later...

Dangling Pointer occurs when a pointer pointing to a variable goes out of scope or when an object/variable's memory gets deallocated. Also, the occurrence of Dangling Pointers can result in some unexpected errors during the execution of a program, so we have to make sure to avoid them while writing a program.

mobdro
 

myfiosgateway.one
 

Edited by teempade04
Link to comment
Short link
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...