How does the compiler get the const address in C ++?

As discussed here What memory area is a const object in C ++? , the compiler will probably not allocate any storage for constants when compiling the code, they can probably be embedded directly into machine code. Then how does the compiler get a permanent address?

C ++ Code:

void f() { const int a = 99; const int *p = &a; printf("constant value: %d\n", *p); } 
+6
source share
3 answers

Whether a constant is allocated to any storage or not depends entirely on the compiler. Compilers are allowed to perform optimizations in accordance with the As-If rule, if the observed behavior of the program does not change, the compiler can allocate storage for a or cannot. Please note that these optimizations are not required, but are allowed by the standard.

Obviously, when you take the address of this const , the compiler will have to return you the address where you can reference a , so it will need to put a in memory, or at least pretend that it is.

+11
source

All variables must be addressable. The compiler can optimize constant variables, but in the case when you use the address of a variable, it cannot do this.

+2
source
Compiler

can do many tricks that are allowed to be done to the extent that it affects the apparent behavior of the program (see the as-if rule ). Thus, it cannot allocate storage for an object const const int a = 99; , but in the case when you take the address of a variable - it needs to allocate some kind of storage or at least pretend it will be, and you will get a memory address that allows you to reference a . code:

 #include <cstdlib> using namespace std; #include <cstdio> const int a = 98; void f() { const int a = 99; const int *p = &a; printf("constant value: %d\n", *p); } int main(int argc, char** argv) { int b=100; f(); return 0; } 

gcc -S main.cpp:

  .file "main.cpp" .section .rodata .LC0: .string "constant value: %d\n" .text .globl _Z1fv .type _Z1fv, @function _Z1fv: .LFB4: .cfi_startproc .cfi_personality 0x3,__gxx_personality_v0 pushq %rbp .cfi_def_cfa_offset 16 movq %rsp, %rbp .cfi_offset 6, -16 .cfi_def_cfa_register 6 subq $16, %rsp movl $99, -4(%rbp) leaq -4(%rbp), %rax movq %rax, -16(%rbp) movq -16(%rbp), %rax movl (%rax), %eax movl %eax, %esi movl $.LC0, %edi movl $0, %eax call printf leave ret .cfi_endproc .LFE4: .size _Z1fv, .-_Z1fv .globl main .type main, @function main: .LFB5: .cfi_startproc .cfi_personality 0x3,__gxx_personality_v0 pushq %rbp .cfi_def_cfa_offset 16 movq %rsp, %rbp .cfi_offset 6, -16 .cfi_def_cfa_register 6 subq $32, %rsp movl %edi, -20(%rbp) movq %rsi, -32(%rbp) movl $100, -4(%rbp) call _Z1fv movl $0, %eax leave ret .cfi_endproc .LFE5: .size main, .-main .section .rodata .align 4 .type _ZL1a, @object .size _ZL1a, 4 _ZL1a: .long 98 .ident "GCC: (Ubuntu/Linaro 4.4.7-2ubuntu1) 4.4.7" .section .note.GNU-stack,"",@progbits 

so that we see that the variable const int a = 99; actually built in machine code, not in a specific area of ​​memory (there is no memory, heap, or data segment allocated for it on the stack). Please correct me if I am wrong.

+1
source

All Articles