Jump to content

Dangling reference

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Dhollander (talk | contribs) at 14:22, 4 April 2006. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A dangling reference in a program is a reference to an object, record or variable which doesn't exist anymore.

In a programming language, a dangling reference occurs when a pointer to a local variable in a function remains after the function has terminated. Another case is a pointer to dynamically allocated memory, right after it is freed and before it is zeroed.

If a dangling pointer is used, undefined behaviour may occur, such as:

  • program crash
  • unknown values being read to (most compilers insert a fixed pattern so the dangling refrence usage is easier to detect).

In relational database systems a dangling reference occurs when a foreign key refers to a record that doesn't exist anymore or was never created. It can be seen as the database equivalent of a dangling pointer.

Examples

A call to the C-program

int *f() {
  int x;
  ...
  return &x;
}

as in

main() {
  int a = *f();
}

returns a pointer to a local variable, which ceased to exist. A dangling reference results.


Consider a database where a foreign key to employee #387 is given and no employee with id 387 exists, this is a situation of a dangling reference.

See also