How are literals stored in memory?

int main() { 5; 1.; ""; } 

    3 answers 3

    From the abstract point of view "in memory" in the C ++ language only lvalues are stored. Of the literals you quoted, only the string literal "" is an lvalue , i.e. only it is stored in memory.

    The rest of the literals are not lvalues and formally have no memory positions.


    In fact, integer literals are usually “embedded” in the context in which they are used. They can be integrated directly into the generated machine command (that is, become part of the code, and not program data), and they can be transformed beyond recognition or dissolve completely in this context: multiplication by 8 can be replaced by a shift to the left by 3 digits, and the assignment of a variable to 0 can be replaced with a machine reset instruction.

    What happens to the floating-point literal depends on the compiler's capabilities and the characteristics of the underlying machine platform. On the x86 platform , in general, the floating literal will actually be stored in memory, i.e. an internal variable will be stored to store it. At the same time, the compiler has the right to recognize "special" floating constants ( 0.0 , 1.0 , etc.) and implement them implicitly. But, once again, in any case, at the level of the language, the floating literal does not have a position in memory.

      Arithmetic literals are not stored in memory, but embedded in object code. String literals are usually stored in a separate literal pool, since they have a static duration of used memory. Identical string literals can be stored as one literal, or as separate literals depending on the compiler options.

      Therefore, this expression in the if clause

       if ( "A" == "A" ) { /* ... */ } 

      may be set to true if these two literals are stored as one literal, that is, they have the same address, or false if the compiler stores each of these literals in a separate memory location.

      As for this program

       int main() { 5; 1.; ""; } 

      Since these sentences of expressions have no side effects, no object code can be generated for them at all.

        No way ... As long as they are not used - for initializing a variable, for example, or there in a function call. And they may not be stored - for example, any a >>= 2 simply turn into a machine command, which simply indicates the immediate value 2, without any storage.

        • and if the variable has already been initialized and we assign it a new literal? the value should be copied - user234830
        • 2
          This is the second question :) Which, generally speaking, depends on the compiler, architecture, etc. For example, if something like const char * s = "Hello, Dolly!"; - then the string will be somewhere in memory, and the pointer will go into the variable. If something like int i = 345; - it could just be an assembler command mov eax, 345 . The fact is that even variables can be stored not in memory, but in registers, for example ... Therefore, it will not be possible to give an absolutely exact answer. - Harry