[to] the difference between a macro definition and a constant definition

Source: Internet
Author: User
Tags constant definition

1. Effect-"High quality C++/C Programming Guide"

The C + + language can use const to define constants, or define constants with # define. But the former has more advantages than the latter:
(1) const constants have data types, and macro constants do not have data types. The compiler can perform type safety checks on the former. Instead of only character substitution, there is no type safety check, and the substitution of characters can produce unexpected errors (marginal effects).
(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants. Rule 5-2-1: Only const constants are used in C + + programs without using macro constants, that is, const constants completely supersede macro constants.

2. Implementation mechanism

A macro is a preprocessing command, that is, a byte substitution during a precompilation phase. A const constant is a variable that, when executed, is defined as a read-only variable that has only one copy during the program's run (because it is a global read-only variable and is stored in a read-only data area of the static store. According to C + + syntax, when you declare the amount to be constant, that tells the program and compiler that you do not want this amount to be modified. The implementation of the program, in order to protect the constants, special will be placed in the protected static storage area. Any attempt to modify the value in this area will be considered illegal and error. This cannot be understood as the usual string is placed in a static storage area. This has nothing to do with the data type, but the question of whether the volume is a variable or a constant. For example, a string variable can be modified. The protection mechanism for this static storage area is implemented by the compiler, not the electrical properties of the memory that stores the value. In other words, the actual memory can always be changed by the user at will, but the compiler to the user's code to inject some of their own protection code, software means to protect the memory soft. This protection can be easily broken down at the assembly level and its protection is ineffective. )。

3. Usage differences

Define macro definition and const constant variable difference:
1.define is a macro definition, and the program is replaced with the content defined by define during the preprocessing phase. Therefore, when the program runs, the constant table does not have a constant defined by define, and the system does not allocate memory for it. const defines a constant that the system allocates memory for when the program runs in a constant table.
2.define defines constants that are only replaced directly when preprocessing. Therefore, data type validation is not possible at compile time. Const-defined constants, which are strictly type-tested at compile time, can avoid errors. 3.define when defining an expression, be aware of the "edge effect", such as the following definition:
#define N 2+3//The N value we expect is 5, so we use n,int a = N/2; The value of a is 2.5, but actually the value of a is 3.5 because in the preprocessing phase, the compiler processes A = N/2 into a = 2+3/2; This is the "edge effect" of the string substitution of the macro definition, so it is defined as follows: #define N (2+3). The const-definition expression does not have the above problem. Constants defined by const are called constant variables for two reasons: const defines constants like variables to check for types, and const can define constants anywhere, and the compiler processes them in a similar way to variables, except where memory is allocated.

Original address: http://www.cnblogs.com/kevinGaoblog/archive/2012/04/16/2452668.html

[to] the difference between a macro definition and a constant definition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.