c - Does the preprocessor prepare a list of unique constant strings before the compiler goes into action? -
in code below, have 2 different local char*
variables declared in 2 different functions.
each variable initialized point constant string, , contents of 2 strings identical.
checking in runtime, variables initialized point same address in memory.
so compiler must have assigned same (constant) value each 1 of them.
how 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; }
it has nothing preprocessor. compiler explicitly allowed (not required) standard share memory identical string literals. details on when happens, must consult compiler's documentation.
for example, here's relevant documentation vc2013:
in cases, identical string literals may pooled save space in executable file. in string-literal pooling, compiler causes references particular string literal point same location in memory, instead of having each reference point separate instance of string literal. enable string pooling, use /gf compiler option.
Comments
Post a Comment