Elephant in Cairo explained

An elephant in Cairo is a term used in computer programming to describe a piece of data that matches the search criteria purposefully inserted at the end of a search space, in order to make sure the search algorithm terminates; it is a humorous example of a sentinel value. The term derives from a humorous essay circulated on the Internet that was published in Byte magazine on September 1989, describing how various professions would go about hunting elephants.

Algorithm

When hunting elephants, the article describes programmers as following this algorithm:

  1. Go to Africa.
  1. Start at the Cape of Good Hope.[1]
  1. Work northward in an orderly manner, traversing the continent alternately east and west,
  1. During each traverse pass:

This algorithm has a bug, namely a bounds checking error: if no elephants are found, the programmer will continue northwards and end up in the Mediterranean sea, causing abnormal termination by drowning.

Thus experienced programmers modify the above algorithm by placing a known elephant in Cairo to ensure that the algorithm will terminate.[2] The modified algorithm is therefore as follows:

  1. Go to Africa.
  1. Put an elephant in Cairo.
  1. Start at the Cape of Good Hope.
  1. Work northward in an orderly manner, traversing the continent alternately east and west,
  1. During each traverse pass:
  1. If you are in Cairo, then there are no elephants in Africa (other than the one you placed there).

See also

References

  1. The Cape of Good Hope has been traditionally believed to be Africa's southernmost point, but that is actually Cape Agulhas.
  2. Book: Steuben, Michael . 1998 . Twenty Years Before the Blackboard . Cambridge University Press . 9780883855256 . 62 .

External links