Magic number (programming)



         


In computer programming, a magic number is a special constant used for some specific purpose. It is called magic because its value or presence is inexplicable without some additional knowledge.

Magic numbers are often chosen based on (among others):

[Top]

Magic numbers in files

An early convention in the Unix operating system was that (binary) files started with two bytes containing a "magic number" identifying the type of the file. These were originally used by the Unix linker and loader. The concept has been expanded on over time, and is now in current use by many other programs across many operating systems. In a wiggly hack, the very earliest magic numbers were PDP-11 branch instructions. The concept of magic numbers can be generalised to all files, since any unencoded binary data is essentially a number; most file formats can thus be identified by some signature that occurs somewhere in the file. Detecting such sequences is therefore an effective way of distinguishing between file formats - and can often yield further information at the same time.

Some examples:

$ readelf -h /bin/ls ELF Header: Magic: 7f 45 4c 46 01 01 01 09 00 00 00 00 00 00 00 00
If we break the hexadecimal values up, the first four denote the first four bytes of the magic number, which basically say it's an ELF binary. The next three are the class, data and version respectively and the eighth bit would seem to denote the ABI (elf(5) and /usr/include/sys/elf_common.h seem to agree with me).

(see for details)

The Unix command file can read and interpret magic numbers from files.

[Top]

Magic numbers in code

The term magic number also refers to the bad programming practice of using numbers directly in source code without explanation. In most cases this makes programs harder to read, understand, and maintain, although most guides make an exception for the numbers zero and one.

For example, to shuffle the values in an array randomly, this pseudocode will do the job:

The following is wikicode, a proposed pseudocode for BambooWeb articles.

for i from 1 to 52 j := i + randomInt(53 - i) - 1 swapEntries(i, j)

The function randomInt(x) chooses a random integer between 1 to x, inclusive, and swapEntries(i, j) swaps the ith and jth entries in the array.

In the above example, 52 is a magic number. It is considered better programming style to write:

var int deckSize := 52 for i from 1 to deckSize j := i + randomInt(deckSize + 1 - i) - 1 swapEntries(i, j)

This is preferred for two reasons:

[Top]

Magic debug values

Magic debug values are specific values written to memory during allocation or deallocation, so that it will later be possible to tell whether or not they have become corrupted and to make it obvious when values taken from uninitialized memory are being used.

Memory is usually viewed in hexadecimal, so common values used are often repeated digits or hexspeak.

Famous and common examples include:

0xBAADF00D 
0xBAADFEED 
0xC0EDBABE 
0xC001D00D 
0xCCCCCCCC 
Used by Microsoft's C++ compiler to mark uninitialised stack areas in debug mode.
0xCDCDCDCD 
Used by Microsoft's C++ debugging heap to mark uninitialised heap areas.
0xDDDDDDDD 
Used by MicroQuill's IBM systems such as the RS/6000, also in OPENSTEP Enterprise and the Commodore Amiga.
0xEBEBEBEB 
From MicroQuill's SmartHeap.
0xFD  
Used by Microsoft's C++ debugging heap to mark class in debug mode.

Note that most of these are each 8 nybbles (32 bits) long, as most modern computers are designed to manipulate 32 bits at a time.

The prevalence of these values in Microsoft technology is no coincidence; they are discussed in detail in Steve McGuire's well-known book





  View Live Article   This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License