Hello, everyone, I am Yu PI, accidentally saw this picture on the Internet:
When I first saw this code, I was surprised that the author used an English word deadbeef to define macro constants!
I thought it was just a humorous programmer’s joke, but later I looked up and realized that the above code is the source code of C++ hash_map! The author also has an ulterior motive for using this particular English word.
Deadbeef is literally deadbeef, but in the programming world it has a deeper meaning. Add a 0x to the word, convert it to uppercase, and you get a typical hexadecimal number: 0xDEADBEEF. This number is often used to identify newly allocated memory that has not yet been initialized; It is also commonly used to indicate a program crash or deadlock in embedded systems, such as IBM RS/6000 systems and Mac OS systems running on 32-bit PowerPC processors.
Then I wonder why such a word was chosen instead of something like “FishPi” (just kidding, hexadecimal up to F).
Check to the net for a while, get the conclusion unexpectedly is: there is no reason, it is a “magic number”!
Magic numbers are constants that appear for no reason and do not need to be explained. So capricious!
In addition to deadbeef, I found many magic numbers, such as:
- 0xBAADF00D (“bad food”) is used by Microsoft’s LocalAlloc (LMEM_FIXED) to indicate uninitialized allocated heap memory when using the debug heap
- 0xDEADC0DE (” Dead code”) is used as a token in OpenWRT firmware, indicating the beginning of the JFFS2 file system to be created at the end of the static firmware
- 0xDEAD10CC (” Dead Lock “deadlock) Indicates an iOS flash abort report
Doesn’t it feel magical? Maybe that’s the romance of the programmer.
See here, I could not help but write a few magic numbers, let’s guess what it means:
redisLock.lease(86400);
if (fileSize > 1073741824) {
...doSomething
}
if (num > 2147483647) {
printf("you lose");
}
Copy the code
So these are the values that we use when we write code, 84600 = 3600 * 24 is a day; 1073741824 = 1024 * 1024 * 1024 indicates 1 GB. And 2147483647 is the maximum value of an int in a programming language like Java.
When I showed the code to my friend, he scoffed: “The magic numbers written by the big man are called magic numbers, while yours are called bad code.”
It is true that we should not use magic numbers when writing code, except in the case of those who are recognized and accepted by the previous generation, because they seriously affect the readability of the code. We can comment these magic numbers by defining constants, such as:
int ONE_DAY = 86400;
int ONE_GB = 1073741824;
int MAX_INTEGER = 2147483647;
Copy the code
This is much clearer and reduces the risk of our typing errors.
In addition to the magic numbers mentioned above, I also saw some practical magic numbers online, such as John Carmack’s magic numbers in Quake:
i = 0x5f3759df - ( i >> 1 );
Copy the code
I can’t believe that this line of code can quickly calculate the inverse of the square root of a number!
Check out the Internet, and there are plenty of papers devoted to this stuff:
Have to sigh the charm of programming, mathematics charm ah! When can I create a magic number that everyone knows?
“Hey, fishskin, stop dreaming and move the bricks!”
“Come, come, I will write you a few magic numbers (bad code)!”
Finally, Yu PI opened a programming learning circle, where there are thousands of friends who are learning programming. I will show you how to do your project live