Question

In the code below, I have two different local char* variables declared in two different functions.

Each variable is initialized to point to a constant string, and the contents of the two strings are identical.

Checking in runtime, the variables are initialized to point to the same address in memory.

So the compiler must have assigned the same (constant) value to each one of them.

How is that possible?

#include <stdio.h>

void PrintPointer()
{
    char* p = "abc";
    printf("%p\n",p);
}

int main()
{
    char* p = "abc";
    printf("%p\n",p);
    PrintPointer();
    return 0;
}
Was it helpful?

Solution

It has nothing to do with the preprocessor. But the compiler is explicitly allowed (not required) by the standard to share the memory for identical string literals. For details on when this happens, you must consult your compiler's documentation.

For example, here's the relevant documentation for VC2013:

In some cases, identical string literals may be pooled to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. To enable string pooling, use the /GF compiler option.

OTHER TIPS

The C++ standard says in N3797 2.14.15/12:

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation defined. The effect of attempting to modify a string literal is undefined.

The C standard now contains the same wording. Historically it was possible to modify string literals at run-time in C, but this is now Undefined Behaviour. Some compilers may allow it, some not.

Technically, the compiler does it by storing string literals in the symbol table. If an identical string is seen more than once, the same symbolic reference is used each time. The same technique might well be used for other literals, but would not be so easily detected.

The preprocessor, by the way, has nothing to do with it.

How is that possible?

It's possible because the compiler keeps track of values like that. But no, the preprocessor generally doesn't get involved in things like this; the preprocessor does things like macro substitutions that modify the code before the compiler starts working. In this case, though, we're talking about actual code:

char* p = "abc";

and that's the domain of the compiler, not the preprocessor.

So the compiler must have assigned the same (constant) value to each one of them. How is that possible?

If you have two identical string literals, as you do here, then the compiler is allowed to combine them into a single one; apparently, your compiler does that. It's also allowed to store them separately.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top