Hello, I’m Yue Chuang.

Recently, I began to review C++ for class preparation and research, while recording some small knowledge points in programming.

One of my readers told me today that when I was testing SVD decomposition of a matrix, I needed to define the size of the rows and columns of the matrix. I used to define these two variables with macro definitions and started thinking about the difference between macro definitions and constants when I was running them.

Referred to some other people’s views, their own here to make a small summary.

The type and security check are different

  • Macro definition is character substitution, there is no difference of data type, at the same time this substitution has no type safety check, may cause errors such as marginal effect;

  • Const constants are type-specific declarations of constants that need to be checked at compile time

The compiler handles it differently

  • Macro definition is a “compile-time” concept, which is developed in the pre-processing stage and cannot be debugged.

  • A const constant is a “runtime” concept that is used during program execution, similar to a read-only row

Different storage methods

  • Macro definition is a direct replacement, does not allocate memory, stored with the program code segment;
  • Const constants need to be allocated memory and stored in the program’s data segment

Different domains

    void f1 (a)
{

    #define N 12
    const int n 12;
    
}

    void f2 (a)
{
    cout<<N <<endl; // True, N is already defined and is not subject to domain constraints
    cout<<n <<endl; // error, n domain is only in f1 function
}
Copy the code

Whether to cancel the definition

  • Macro definitions can be invalidated by #undef

  • A const constant, once defined, is permanent in its domain

void f1(a)
{
  #define N 12
  const int n = 12;
  
  #undef N // When macros are undefined, N is invalid even in f1 functions
  #define N 21// It can be redefined after cancellation
}
Copy the code

Whether you can make function arguments

  • Macro definitions cannot be passed to functions as arguments

  • Const constants can appear in function argument lists

Multi-angle analysis

In terms of defining constants, const is a variable with a type. #define is a constant without a type.

Angle 2: In terms of phases, #define is used during the preprocessing phase of compilation, while const is used at compile/run time.

Angle 3: In terms of the way it works, #define is just a simple string substitution with no type checking. Const, on the other hand, has a corresponding datatype and is evaluated to avoid some low-level errors. Because define is a simple string substitution, boundary effects can occur. For an example, see the following code:

#define N 2+3     // We expected N to be 5, we use it this way
double a = N/2.0;  // We expected the value of A to be 2.5, but it is actually 3.5
Copy the code

Angle 4: In terms of space occupancy, for example:

#define3.14 PI?// Preprocessing takes up code segment space
const float PI=3.14;    // Is essentially a float that takes up space in the data segment
Copy the code
#include <iostream>

int main(a) {
	#define3.1415926 PI?
	const float PI2 = 3.1415926;
	std::cout << sizeof(PI) << sizeof(PI2);
	return 0;
}
Copy the code

Angle 5: For code debugging convenience, const constants can be debugged. #define cannot be debugged because it is replaced during precompilation

Const cannot be redefined. #define can be undefined by #undef and then redefined.