In computer programming, a magic number is a special constant used for some specific purpose. It is called magic because it is hardcoded in and its value or presence is inexplicable without some additional knowledge.
Magic numbers are often chosen based on (among other factors):
0x12345678)
Many other types of files have content that identifies the type. When interpreted as a number, that content can be considered the "magic number" associated with that type. Detecting such numbers in file content is therefore an effective way of distinguishing between file formats - and can often yield further information at the same time.
Some examples:
0xCAFEBABE on big-endian systems.
0x474946383961) or 'GIF87a' (0x474946383761)
0xFFD8FF, and JPEG/JFIF files contain the ASCII code for 'JFIF' (0x4A464946) or JPEG/EXIF files contain the ASCII code for 'Exif' (0x45786966) beginning at byte 6 in the file, followed by more metadata about the file.
\211 P N G \r \n \032 \n (0x89504e470d0a1a0a)
0x4D546864) followed by more metadata about the file.
0x2321, or 0x2123 on little-endian processors) followed by the path to an interpreter.
0x2521).
0x4D5A), the initials of the designer of the file format, Mark Zbikowski.
0x19540119 or 0x011954 depending on version; both represent the birthday of author Marshall Kirk McKusick.
0x4A6F7921) as a prefix.
0x2A00 or 0x002A (decimal 42 as a 2-byte integer in Intel or Motorola byte ordering).
The Unix program file can read and interpret magic numbers from files, and indeed, the file which is used to parse the information is called magic.
0x2A.
For example, to shuffle the values in an array randomly, this pseudocode will do the job:
for i from 1 to 52 j := i + randomInt(53 - i) - 1 a.swapEntries(i, j)
where a is an array object, 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 this example, 52 is a magic number. It is considered better programming style to write:
constant int deckSize := 52 for i from 1 to deckSize j := i + randomInt(deckSize + 1 - i) - 1 a.swapEntries(i, j)
This is preferable for several reasons:
52 mean here? Why 52? The programmer might infer the meaning after reading the code carefully, but it's not obvious. Magic numbers become particularly confusing when the same number is used for different purposes in one section of code.
52 in the program with 78. This would cause two problems. First, it would miss the value 53 on the second line of the example, which would cause the algorithm to fail in a subtle way. 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 variable in the second example would be a simple, one-line change.
deckSize into a parameter of that procedure. The first example would require several changes, perhaps:
function shuffle (int deckSize) for i from 1 to deckSize j := i + randomInt(deckSize + 1 - i) - 1 a.swapEntries(i, j)
Disadvantages are:
Memory is usually viewed in hexadecimal, so common values used are often repeated digits or hexspeak.
Famous and common examples include:
0x..FACADE : Used by a number of RTOSes
0xABABABAB : Used by Microsoft's LocalAlloc() to mark "no man's land" guard bytes after allocated heap memory
0xBAADF00D : Used by Microsoft's LocalAlloc(LMEM_FIXED) to mark uninitialised allocated heap memory
0xBADBADBADBAD : Burroughs B6700 "uninitialized" memory (48-bit words)
0xBADC0FFEE0DDF00D : Used on IBM RS/6000 64-bit systems to indicate uninitialized CPU registers
0xC001D00D
0xC0DEBABE
0xC0EDBABE
0xCACACACA
0xCAFECAFE
0xCAFEFEED : Used by Sun Microsystems' Solaris debugging kernel to mark kmemfree() memory
0xCCCCCCCC : Used by Microsoft's C++ debugging heap to mark uninitialised stack memory
0xCDCDCDCD : Used by Microsoft's C++ debugging heap to mark uninitialised heap memory
0xDDDDDDDD : Used by MicroQuill's SmartHeap and Microsoft's C++ debugging heap to mark freed heap memory
0xDEADBABE : Used at the start of Silicon Graphics' IRIX arena files
0xDEADBEEF : Famously used on IBM systems such as the RS/6000, also in OPENSTEP Enterprise and the Commodore Amiga
0xDEADDEAD : A Microsoft Windows STOP Error code used when the user manually initiates the crash.
0xDECAFBAD
0xEBEBEBEB : From MicroQuill's SmartHeap
0xFDFDFDFD : Used by Microsoft's C++ debugging heap to mark "no man's land" guard bytes before and after allocated heap memory
0xFEEDBABE
0xFEEDFACE : Seen in Mach-O binaries on Apple Computer's Mac OS X platform
0xFEEEFEEE : Used by Microsoft's HeapFree() to mark freed heap memory
Note that most of these are each 8 nibbles (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 Maguire's well-known book Writing Solid Code from Microsoft Press. He gives a variety of criteria for these values, such as:
Since they were often used to mark areas of memory that were essentially empty, some of these terms came to be used in phrases meaning "gone, aborted, flushed from memory"; e.g. "Your program is DEADBEEF".
Pietr Brandehörst's ZUG programming language initialized memory to either 0x0000, 0xDEAD or 0xFFFF in development environment and to 0x0000 in the live environment, on the basis that uninitialised variables should be encouraged to misbehave under development to trap them, but encouraged to behave in a live environment to reduce errors.
For this reason these two numbers are considered valid Magic Numbers.
Magische Zahl | Magic number | マジックナンバー (プログラム) | Número mágico
This article is licensed under the GNU Free Documentation License.
It uses material from the
"Magic number (programming)".
Home Page • arts • business • computers • games • health • hospitals • home • kids & teens • news • physicians • recreation• reference • regional • science • shopping • society • sports • world