The difference between define and const in C + + __c++

Source: Internet
Author: User
Tags float max

The difference is as follows:

Constants defined with #define MAX 255 are of no type , given an immediate number, and 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. Copy replacement is used where the constant is applied in the program;

with const float MAX = 255; The defined constants have type names, are stored in the static area of memory, the const variable has only one copy while the program is running, and the macro variable defined by #define has multiple copies. Therefore, the macro definition consumes more memory than the const variable in the process of running the program.

Constants defined with define can not be pointed to with pointer variables, constants defined with const are the addresses of the constants that can be pointed to by the pointer;

You can define simple functions with define, and const is not allowed to define functions.

Specifically, there are the following differences:

1. Compiler processing Mode
define– in the preprocessing phase
const– determines its value at compile time

2. Type check
define– no type, no type safety check, may produce unexpected errors
const– with data types, type checking at compile time

3. Memory space
define– does not allocate memory, gives the immediate number, how many times the use of the number of replacements, in memory there will be more than one copy, consuming large memory
const– allocates space in a static storage area, with only one copy in memory during program operation

4. Other
At compile time, the compiler usually does not allocate storage space for the const constants , but instead stores them in the symbol table , which makes it a constant during compilation , without the storage and reading memory operations, Makes it very efficient, too.
A macro substitution is a substitution, not a calculation, or an expression solution.

The scope of a macro definition is limited to the current file.
By default, the const object is valid only within a file, and when a const variable with 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 among multiple files, you must add the extern keyword (both declared and defined) before the variable definition.

Rules
Use only const constants in C + + programs without using macro constants, which are the const constants that completely replace macro constants .



http://blog.csdn.net/yingyujianmo/article/details/51206460

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.