Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Dangling pointer
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Cause of dangling pointers== In many languages (e.g., the [[C (programming language)|C programming language]]) deleting an object from memory explicitly or by destroying the [[stack frame]] on return does not alter associated pointers. The pointer still points to the same location in memory even though that location may now be used for other purposes. A straightforward example is shown below: <syntaxhighlight lang="C"> { char *dp = NULL; /* ... */ { char c; dp = &c; } /* c falls out of scope */ /* dp is now a dangling pointer */ } </syntaxhighlight> If the operating system is able to detect run-time references to [[null pointer]]s, a solution to the above is to assign 0 (null) to dp immediately before the inner block is exited. Another solution would be to somehow guarantee dp is not used again without further initialization. Another frequent source of dangling pointers is a jumbled combination of <code>malloc()</code> and <code>free()</code> library calls: a pointer becomes dangling when the block of memory it points to is freed. As with the previous example one way to avoid this is to make sure to reset the pointer to null after freeing its reference—as demonstrated below. <syntaxhighlight lang="C"> #include <stdlib.h> void func() { char *dp = malloc(A_CONST); /* ... */ free(dp); /* dp now becomes a dangling pointer */ dp = NULL; /* dp is no longer dangling */ /* ... */ } </syntaxhighlight> An all too common misstep is returning addresses of a stack-allocated local variable: once a called function returns, the space for these variables gets deallocated and technically they have "garbage values". <syntaxhighlight lang="C"> int *func(void) { int num = 1234; /* ... */ return # } </syntaxhighlight> Attempts to read from the pointer may still return the correct value (1234) for a while after calling <code>func</code>, but any functions called thereafter may overwrite the stack storage allocated for <code>num</code> with other values and the pointer would no longer work correctly. If a pointer to <code>num</code> must be returned, <code>num</code> must have scope beyond the function—it might be declared as <code>[[static variable|static]]</code>.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)