Opentopia Directory Encyclopedia Tools

Magic number (programming)

Encyclopedia : M : MA : MAG : Magic number (programming)


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):

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. It is a form of in-band signaling.

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:

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.

Magic numbers in protocols

Please [http://encycl.opentopia.com/ expand and improve] this section as described on this article's or at [Requests for expansionRequests for expansion], then remove this message.

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, it is a good idea to define all other numbers in code as named constants.

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:

function shuffle (int deckSize)
for i from 1 to deckSize
j := i + randomInt(deckSize + 1 - i) - 1
a.swapEntries(i, j)
Disadvantages are:

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:

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.

Allowed use of Magic Numbers

Although still controversial, most programmers would conceed that the use of 0 (zero) and 1 are the only two allowable magic numbers in general code. This is for numerous reasons. 0 and 1 mean something in themselves.

eg.
for index := 0 to list.count -1 do
DoSomething();
For this reason these two numbers are considered valid Magic Numbers.

See also

Magic number section in 'File format' article

References

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: