Avoiding Dangling Pointer Errors
In C/C++, the simplest technique is to implement an alternative version of the free (or alike) function or delete destructor which guarantees the reset of the pointer. However, this technique will not clear other pointer variables which may contain a copy of the pointer.
/* Alternative version for 'free' */ void safefree(void **pp) { if (pp != NULL && *pp != NULL) { /* safety check */ free(*pp); /* deallocate chunk */ *pp = NULL; /* reset original pointer */ } } int f(int i) { char *p = NULL, *p2; p = (char *)malloc(1000); /* get a chunk */ p2 = p; /* copy the pointer */ /* use the chunk here */ safefree(&p); /* safety freeing; does not affect p2 variable */ safefree(&p); /* this second call won't fail */ char c = *p2; /* p2 is still a dangling pointer, so this is undefined behavior. */ }The alternative version can be used even to guarantee the validity of an empty pointer before calling malloc:
safefree(&p); /* i'm not sure if chunk has been released */ p = malloc(1000); /* allocate now */These uses can be masked through #define directives to construct useful macros, creating something like a metalanguage or can be embedded into a tool library apart. In every case, programmers using this technique should use the safe versions in every instance where free would be used; failing in doing so leads again to the problem. Also, this solution is limited to the scope of a single program or project, and should be properly documented.
Among more structured solutions, a popular technique to avoid dangling pointers is to use smart pointers. A smart pointer typically uses reference counting to reclaim objects. Some other techniques include the tombstones method and the locks-and-keys method.
Another approach is to use the Boehm garbage collector, a conservative garbage collector that replaces standard memory allocation functions in C and C++ with a garbage collector. This approach completely eliminates dangling pointer errors by disabling frees, and reclaiming objects by garbage collection.
In languages like Java, dangling pointers cannot occur because there is no mechanism to explicitly deallocate memory. Rather, the garbage collector may deallocate memory, but only when the object is no longer reachable from any references.
Read more about this topic: Dangling Pointer
Famous quotes containing the words avoiding, dangling, pointer and/or errors:
“The only sure way of avoiding these evils [vanity and boasting] is never to speak of yourself at all. But when, historically, you are obliged to mention yourself, take care not to drop one single word that can directly or indirectly be construed as fishing for applause.”
—Philip Dormer Stanhope, 4th Earl Chesterfield (16941773)
“And, indeed, is there not something holy about a great kitchen?... The scoured gleam of row upon row of metal vessels dangling from hooks or reposing on their shelves till needed with the air of so many chalices waiting for the celebration of the sacrament of food. And the range like an altar, yes, before which my mother bowed in perpetual homage, a fringe of sweat upon her upper lip and the fire glowing in her cheeks.”
—Angela Carter (19401992)
“The hardiest skeptic who has seen a horse broken, a pointer trained, or has visited a menagerie or the exhibition of the Industrious Fleas, will not deny the validity of education. A boy, says Plato, is the most vicious of all beasts; and in the same spirit the old English poet Gascoigne says, A boy is better unborn than untaught.”
—Ralph Waldo Emerson (18031882)
“[Montesquieu] lifted the veil from the venerable errors which enslaved opinion, and pointed the way to those luminous truths of which he had but a glimpse himself.”
—James Madison (17511836)