Type Qualifier

박영재·2024년 9월 27일

Type Qualifier

Programmer's Responsibility for Compiler Optimization

Type qualifiers provide hints to the compiler for optimizing code by specifying how a variable should be treated.

  • const: Informs the compiler that a variable’s value will not change, allowing the compiler to evaluate expressions involving the variable at compile time.
  • volatile: Prevents the compiler from optimizing code that depends on the variable, ensuring the variable's value is always read from memory and not cached in a register.

Example

int a = 10;
int b = a;
// No new allocation for a and b
// b is used
// The compiler may optimize and replace b with a
int* a = (int*)malloc(sizeof(int));
*a = 10;
int b = *a;
// No new allocation for a and b
// b is used
// The compiler can't replace b with *a because *a might be modified indirectly
// For example: int* c = a; *c = 20;
profile
People live above layers of abstraction beneath which engineers reside

0개의 댓글