c++ - can anyone explain where the constants or const variables stored? -
as know, c++'s memory model can divided 5 blocks: stack, heap, free blocks, global/static blocks, const blocks. can understand first 3 blocks , know variables static int xx
stored in 4th blocks, , "hello world"-string constant, stored in 5th blocks-const blocks? , , int = 10
, "10" stored? can explain me?
thanks lot.
there difference between string literals , primitive constants. string literals stored code in separate area (for historical reasons block called "text block"). primitive constants, on other hand, special: can stored in "text" block well, values can "baked" code itself. example, when write
// global integer constant const int = 10; int add(int b) { return b + a; }
the return expression translated piece of code not reference a
@ all. instead of producing binary code looks this
load r0, <stack>+offset(b) load r1, <address-of-a> add r0, r1 ret
the compiler may produce this:
load r0, <stack>+offset(b) add r0, #10 ; <<== here #10 means "integer number 10" ret
essentially, despite being stored rest of constants, a
cut out of compiled code.
as far integer literals constants go, have no address @ all: "baked" code: when reference them, instructions load explicit values generated, in same way shown above.
Comments
Post a Comment