C + + define and const

Source: Internet
Author: User
Tags float max

Not only can you define constants in C + + define , but you can also const define constants with the following differences:

    • A #define MAX 255 defined constant is no type, given an immediate number, the compiler simply links the defined constant value to the name of the defined constant, define the defined macro variable to be replaced at the time of preprocessing, and a copy substitution where the constant is used in the program;

      const float MAX = 255;a defined constant has a type name, which is stored in the static area of the memory, the const variable has only one copy while the program is running, and the macro variable defined by # define has multiple copies, so the macro definition consumes much more memory than the const variable during the program's run;

    • Constants defined with define can not be pointed to by pointer variables, constants defined by const are pointers to the address of the constant;

    • With define, you can define some simple functions, and const cannot define functions.

Specifically, there are the following differences:

1. Compiler processing Method
define– replacement in the preprocessing phase
const– to determine its value at compile time

2. Type checking
define– no type, no type safety checks, may produce unexpected errors
const– has a data type and is type checked at compile time

3. Memory space
define– does not allocate memory, gives immediate number, how many times it is used to replace, there will be multiple copies in memory, memory consumption is large
const– allocates space in the static store with only one copy in memory during program run

4. Other
At compile time, the compiler typically does not allocate storage space for const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the storage and read memory operations, making it highly efficient.
Macro substitution is only used for substitution, no calculation, no expression solving.

The scope of the macro definition is limited to the current file.
By default, a const object is only valid within a file, and when a const variable of the same name appears in multiple files, it is equivalent to defining separate variables in different files.
If you want to share a const object between multiple files, you must add the extern keyword before the variable definition (both when declaring and defining).

C + + define and const

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.