The similarities and differences between Const and # define

Source: Internet
Author: User

1 similarities and differences as constants (0) Same

Both can be used to define constants;

#define PI 3.14159//Macro Constants
Const DOULBE pi=3.14159; Constant

(1) different compiler processing methods

The Define macro is expanded in the preprocessing stage;

Const constants are used in 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

Define macros do not allocate memory when they are defined; Define macros are only expanded, how many places are used, and how many times they are expanded;

Const constants are allocated in memory when defined (either in the heap or in the stack);

(4) Allocation of space when assigning values

const   saves space and avoids unnecessary memory allocations . For example:   
         #define PI 3.14159//Macro constants   
         const Doulbe pi=3.14159; Pi is not placed in ROM ...   
        double i=pi;//The Pi allocates memory at this time, No more assignments later!   
        double i=pi;//macro substitution during compilation, allocation of memory   
        double J=pi; There is no memory allocation   
        double j=pi;//macro replacement again, allocate memory again!   
         Const defines constants from a compilation perspective, Just give the corresponding memory address instead of the immediate number as given by #define , so const The constants defined have only one copy during the program's run, and the constants defined by   #define have several copies in memory.  

(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.

2 comparison of the two in C + +

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.

"Rule 5-3-1" need to be exposed to the constant 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.

"Rule 5-3-2" if a constant is closely related to other constants, you should include this relationship in your definition, rather than giving some orphaned values. For example:

const float RADIUS = 100;

const FLOAT DIAMETER = RADIUS * 2;

2 #define其他功能2.1 Simple macro definition

#define MAXTIME 1000; Functions like Const constants

2.2 with parameter macro definition

Define can accept some parameters like a function, as follows:

#define MAX (x, y) > (y)? (x):(y);

It will return the larger of the two numbers, and this "function" has no type checking, just like a function template, and of course, it is not difficult to see that it is absolutely not as safe as a template.

2.3 Multi-line macro definition

Define can replace multiple lines of code, such as the macro definition in MFC (very classic, although it makes people look nauseous):

#define MACRO (arg1, arg2) do {/* * */////*    */  while (0*/*

The key is to add a "/" to each line break.

2.4-Piece compilation

In the large-scale development process, especially cross-platform and system software, the most important function of define is conditional compilation.

#ifdef  WINDOWS//  do something#endif#ifdef    LINUX// do something  #endif  #ifdef  dv22_aux_input#define  aux_mode 3#else #define  Auy_mode 3#endif

Compile environment can be set by # define at compile time

3 Const Other Features

A constant type is a type that uses the const description of a type modifier, and the value of a variable or object of a constant type cannot be updated.

The similarities and differences 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.