Where and how are constants stored?

I read this question from here , and I also read the related question from c-faq , but I don't understand the exact reason for this: -

#include <iostream>

    using namespace std;

    int main()
    {
        //const int *p1 = (int*) &(5);  //error C2101: '&' on constant
        //cout << *p1;

        const int five = 5;
        const int *p2 = &(five);
        cout << *p2 << endl;

        char *chPtr = (char*) &("abcde");
        for (int i=0; i<4; i++) cout << *(chPtr+i);
        cout << endl;
        return 0;
    }

, , , . , . "abcde", , (chPtr), , , , chPtr , , , . const int "five" p2 ?

"five", : &(5)? "5" "five" -? "5" ?

+3
3

(, &(5)), "" - . , MIPS64 :

DADDUI R1, R1, #5

, () , .

const int i = 5 , (, , ) 5 . i, , , . , , , ( const int i, pass, , - ).

- .

+6

" ", , , . , (, - ). "constexpr", mechansim: , , , . , , - , , !

, , :

int main(int argc, char** argv)
{
  const int a = 2;
  const int b = 3;
  const int c = a+b;

  volatile int d = 6;
  volatile int e = c+d;

  std::cout << e << std::endl;
  return 0;
}

, :

    37    const int a = 2;
    38    const int b = 3;
    39    const int c = a+b;
    40
    41    volatile int d = 6;
0x400949  <+0x0009>         movl   $0x6,0x8(%rsp)
    42    volatile int e = c+d;
0x400951  <+0x0011>         mov    0x8(%rsp),%eax
0x400955  <+0x0015>         add    $0x5,%eax
0x400958  <+0x0018>         mov    %eax,0xc(%rsp)
    43
    44    std::cout << e << std::endl;
0x400944  <+0x0004>         mov    $0x601060,%edi
0x40095c  <+0x001c>         mov    0xc(%rsp),%esi
0x400960  <+0x0020>         callq  0x4008d0 <_ZNSolsEi@plt>
    45    return 0;
    46  }

(volatile , ). 41, c, LITERAL 0x5, , . 37-39 .

, :

int main(int argc, char** argv)
{
  const int a = 2;
  const int b = 3;
  const int c = a+b;

  volatile int d = 6;
  volatile int e = c+d;
  volatile int* f = (int*)&a;
  volatile int g = *f;

  std::cout << e << std::endl;
  std::cout << g << std::endl;
  return 0;
}


    37        const int a = 2;
0x400955  <+0x0015>         movl   $0x2,(%rsp)
    38        const int b = 3;
    39        const int c = a+b;
    40
    41        volatile int d = 6;
0x400949  <+0x0009>         movl   $0x6,0x4(%rsp)
    42        volatile int e = c+d;
0x400951  <+0x0011>         mov    0x4(%rsp),%eax
0x40095c  <+0x001c>         add    $0x5,%eax
0x40095f  <+0x001f>         mov    %eax,0x8(%rsp)
    43        volatile int* f = (int*)&a;
    44        volatile int g = *f;
0x400963  <+0x0023>         mov    (%rsp),%eax
0x400966  <+0x0026>         mov    %eax,0xc(%rsp)
    45
    46        std::cout << e << std::endl;
0x400944  <+0x0004>         mov    $0x601060,%edi
0x40096a  <+0x002a>         mov    0x8(%rsp),%esi
0x40096e  <+0x002e>         callq  0x4008d0 <_ZNSolsEi@plt>
    47        std::cout << g << std::endl;
0x40097b  <+0x003b>         mov    0xc(%rsp),%esi
0x40097f  <+0x003f>         mov    $0x601060,%edi
0x400984  <+0x0044>         callq  0x4008d0 <_ZNSolsEi@plt>
    48        return 0;

, , a , ( cz rsp). ... c a, , c, 5! ? , , - , . , NOT 2, , , , 2. , a 37 , a 43.

, ? , . CRAZY.

(btw, g++ -g -O2, / -, , , , .)

+3

Here is an example of taking the address const intand demonstrating that (in gcc on my machine, at least) it is stored as a local (not global static) variable.

#include <iostream>

const int *func() {
    const int five = 5;
    const int *p = &(five);
    std::cout << *p << '\n';
    return p;
}

// function to overwrite stack values left by earlier function call
int func2(int n, int x) {
    for (int i = 0; i < x; ++i)
        n *= 2;
    return n;
}

int main() {
    const int *p = func();
    std::cout << func2(2, 10) << '\n';
    std::cout << *p << '\n';
    return 0;
}

Output Example:

5
2048
1
+1
source

All Articles