Dangling Pointer - Avoiding Dangling Pointer Errors

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:

    If men as individuals surrender to the call of their elementary instincts, avoiding pain and seeking satisfaction only for their own selves, the result for them all taken together must be a state of insecurity, of fear, and of promiscuous misery.
    Albert Einstein (1879–1955)

    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 (1940–1992)

    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 (1803–1882)

    My errors are by now natural and incorrigible; but the good that worthy men do the public by making themselves imitable, I shall perhaps do by making myself evitable.
    Michel de Montaigne (1533–1592)