| |||||||||
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):
0x12345678)
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:
0xCAFEBABE.
0x474946383961) or 'GIF87a' (0x474946383761)
0x4A464946) followed by more metadata about the file.
0x4D546864) followed by more metadata about the file.
0x2321) followed by the path to an interpreter.
0x4D5A)
(see for details)
The Unix command file can read and interpret magic numbers from files.
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.
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:
This is preferred for two reasons:
52 mean here? Why 52? The programmer might infer the meaning after reading the code carefully, but it's not obvious.
52 in the program with 78. This would have two problems. First, it would miss the value 53 on the second line of the example, which would cause the algorithm to fail. Second, it would likely replace the characters 52 everywhere, regardless of whether they refer to the deck size or to something else entirely, which could introduce bugs. By contrast, changing the value of the DeckSize constant in the second example would be a simple, one-line change.
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 0xCDCDCDCD 0xDDDDDDDD 0xEBEBEBEB 0xFD 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