Magic number (programming) |
: For other uses of the term, see magic number .
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 other factors):
= Magic numbers in files =
An early convention in the Unix operating system was that (Binary numeral system) 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 (computing). The concept has been expanded on over time, and is now in current use by many other programs across many operating systems. It is a form of in-band signaling.
In a quick Hack (technology slang), 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:
The Unix program file can read and interpret magic numbers from files.
= 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:
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 i th and j th 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 at least five reasons:
Magic numbers are also great candidates of becoming Variable#Constant.
= Magic debug values =
Magic debug values are specific values written to random Access Memory during memory 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 *0xBADBADBADBAD : Burroughs B6700 uninitialized memory (48-bit words) *0xC001D00D *0xC0DEBABE *0xC0EDBABE *0xCACACACA : In spanish, caca is a pre-teenager use word equivalent to feces *0xCAFECAFE *0xCCCCCCCC : Used by Microsoft s Visual C Plus Plus Compiler to mark uninitialised stack (computing) areas in debug mode *0xCDCDCDCD : Used by Microsoft s C++ debugging dynamic memory allocation to mark uninitialised heap areas *0xDDDDDDDD : Used by MicroQuill s SmartHeap and Microsoft s C++ debugging heap to mark memory returned to the heap *0xDEADBEEF : Famously used on IBM systems such as the RS/6000, also in OPENSTEP Enterprise and the Commodore International Amiga *0xEBEBEBEB : From MicroQuill s SmartHeap *0x..FACADE : Used by a number of real-time operating systemes *0x......FD : Used by Microsoft s C++ debugging heap to mark guard bytes in the heap *0xFEEDBABE *OxFEEDFACE : Seen in Mach-O binaries on Apple Computers Mac OS X platform *0xFEEEFEEE : Used by Microsoft s C++ compiler to mark the storage area of a deleted class (computer science) 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 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 initialised 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.
=References=
|
|