#include<stdio.h> void foo(char *mystr) { int a; /*code goes here*/ #ifdef CHECK int local_var; printf(" strings address %p\n",mystr); printf("local variables address %p \n",&local_var); puts(""); puts(""); #endif return; } int main() { char a[]="hello"; char *b="hello"; foo(a); foo(b); foo("hello"); }
When compiling with gcc -DCHECK prog_name.c and executing on my Linux machine, the following output appears ...
strings address 0xbfdcacf6 local variables address 0xbfdcacc8 strings address 0x8048583 local variables address 0xbfdcacc8 strings address 0x8048583 local variables address 0xbfdcacc8
for the first case, when a string is defined and initialized in the "correct path for mutable strings", the difference between the addresses is 0x2E (5 bytes).
in the second case, when the string is defined as char * p = "hi", the differences in the addresses are 0xB7D82745.Thats are larger than the size of my stack.so, I am sure that the string is not on the stack. This is the only place you can find the .rodata section.
The third example is similar to the case
PS: As mentioned above, this is not portable, but the original question hardly leaves room for portability by specifying .rodata strong> :)
source share