The difference between const and # define

Source: Internet
Author: User
Tags constant definition

I often encounter defining global constants when I brush a question. I usually use # define (probably because of the very little contact with the const reason)

As usual, when we did 51nod1082 yesterday, the violence was defined most by # define. But the submission timed out .....

Later to see others write when the definition of the global use is a const so Baidu the difference between the two

(1) different compiler processing methods

Define macros are expanded during the preprocessing phase.

Const constants are used during the compile run phase.

(2) Different types and security checks

Define macros have no type, do not do any type checking, just expand.

Const constants have specific types that perform type checking during the compilation phase.

(3) different storage methods

The Define macro is only expanded, how many places are used, how many times it is expanded, and no memory is allocated.

Const constants are allocated in memory (either in the heap or in the stack).

(4) const can save space and avoid unnecessary memory allocation. For example:
#define PI 3.14159//Macro Constants
Const DOULBE pi=3.14159; Pi is not placed in ROM at this time ...
Double I=pi; Allocate memory for PI at this time, no longer assigned!
Double I=pi; Macro substitution during compilation, allocating memory
Double J=pi; No memory Allocations
Double J=pi; Another macro replacement, another memory allocation!
Const definition constants from the assembly point of view, just give the corresponding memory address, instead of the immediate number as given in # define, so the const definition of the constant in the program run only one copy, and #define定义的常量在内存中有若干个拷贝.
(5) Improved efficiency. The compiler typically does not allocate storage space for ordinary 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.

Const and #define的比较

The C + + language can use const to define constants, or you can use #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" uses only const constants in C + + programs without using macro constants, that is, const constants completely replace macro constants.

5.3 Constant Definition Rules

L "rule 5-3-1" need to be exposed to the external constants in the header file, do not need to be exposed to the external constants in the definition file header. For ease of management, the constants of different modules can be centrally stored in a common header file.

L "rule 5-3-2" if a constant is closely related to other constants, the relationship should be included in the definition, rather than some orphaned values.

Reference Blog: http://blog.sina.com.cn/s/blog_79b01f6601018xdg.html

The difference between const and # define

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.