Define defined constants and const constants in C + +

Source: Internet
Author: User

Constants are the amount that cannot be changed in a program, there are two ways to define constants in C + +, one is to use the Define macro definition, and one is the new const type constant variable in C + +, the following mainly discusses the related problems between them;

Constants defined by define:

Define is a preprocessing directive that defines a macro, which is simply a substitution that replaces the value of a macro variable, as in the following code:

#define Num 2int  main () {    printf ("%d", NUM);}

The

compiler does not process this code at compile time, and the compiler will first process the preprocessing directives, generate the relevant code files based on the preprocessor directives, and compile the file. Get the associated. obj file, and finally get an executable file by linking the associated. obj file, most typically the # include directive that we write in the. cpp file, which, when processed, first copies the entire header file you want to include in the. cpp file and replaces this # Include directive, and then compile the resulting file, the intermediate file is suffixed with. I in Windows, and in Visual C + + 6.0 Click Project-->settings-->c/c++ in the Project The options last line with '/P ' (P is uppercase) so that when you click the Compile button will not compile the generated obj file, will only generate. i files, through this. I file can be seen in the preprocessing when the NUM is replaced with 2 and then in the compilation process, this time click on the generated error, Because we have modified the compile option without generating the. obj file but this file is required at the time of build, it will cause an error, so remove this/p option from the build. And we see that there is no such substitution when using the const definition, which is the same as using a normal variable. The const variable only qualifies the value of the variable at the syntactic level, and we can modify the value of the variable by forcing the type conversion or by inline assembly, such as the following code:

 int  Main (int  argc, char  * argv[]) { const  nnum = 10  ;  int  *pnum = (int  *) &nnum; printf (  %d\n   "  return  0  ;}  
    Ten ;     __asm    {        mov4    }    printf (" %d\n ", Nnum);    0 ;

But we see that after these two methods are modified, the output value is still 10, for this reason we can view the disassembly code by looking at

 ;  printf ("%d\n", nnum);  00401036  push   0Ah  00401038  push  offset string  " %d\n  "  ( 0042001c  )   Call  printf (00401070  )  00401042  add  esp,8  

When calling printf, the argument to the stack is 10, there is no nnum worth the relevant operation, in the use of const-defined constants, the compiler thinks that since this is a constant, should not be modified, in order to improve efficiency, in the use of the corresponding memory will not be addressed, Instead, replace it directly with the value at the time of initialization, in order to prevent this from happening, you can take advantage of the keyword in C + +: volatile. This keyword is guaranteed to be read in memory every time the variable is used.

We can summarize a few differences between the const and the define:

1) Define is a preprocessing directive, and const is a keyword.

2) The constant compiler defined by define does not perform any checks, and const-defined constant compilers perform type checking and are relatively safer than define

3) Define macros are replaced without memory when used, while const is a variable that occupies memory space

4) define defined macros are not addressable in the code snippet, and const-defined constants are addressable, either in a data segment or in a stack segment.

5) define defined macros are replaced during pre-compilation operations, while const-defined variables are determined at compile time

6) The macro defined by define is a true constant and will not be modified, and the const definition is actually a variable that can be modified by the relevant means.

Define defined constants and const constants in C + +

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.