I have been programming C for a while (but still pretty new to C) and I am sometimes getting confused of the way the C is handling the memory.
Consider following valid C snippet:
const char *string(void)
{
/* where is this pointer variable located in the memory? */
const char *s;
/* where is this text data located in the memory? */
/* and when the program allocates memory for it? */
s = "Hello, World";
return s;
}
int main(void)
{
printf( "%s", string() );
return 0;
}
I am asking what exactly is going on in the memory?
Isn't the pointer variable 's' a local variable or where is the pointer variable stored in the memory. Also where is the text constant "Hello, World" stored in memory (isn't this considered to be local variable which isnt accesible after function returns)?
Basically what kind of variables / data are consider to be in "local" scope of the functions (ceases to be accessible after function returns)?
I hope you understand what I am trying to say :D.. I think I have lot to learn about compilers and executables so feel free enlighten me!
I am asking what exactly is going on in the memory?
Local variables are being allocated on the stack.
Constants, including the literal strings, are being allocated in the text or data sections of the executable.
Isn't the pointer variable 's' a local variable?
Yes
or where is the pointer variable stored in the memory?
The local s
is in a register or on the stack.
Also where is the text constant "Hello, World" stored in memory?
In either the .text or the .data section. It's constant, but legacy code would sometimes modify them, so it depends on compiler options. You need to distinguish between the reference and the object to understand it all.
(isn't this considered to be local variable which isn't accesible after function returns)?
Well, s
is local but the string itself will be needed every time the function is called, and the local frame won't even exist until that happens, so the constant itself is most likely stored in the .text section. It may be stored in .data, depending on compiler options and how much the current compiler release cares about compiling legacy code. The literal inside the expression is something completely different from the variable it is being assigned to.
Basically what kind of variables / data are consider to be in "local" scope of the functions (ceases to be accessible after function returns)?
The ones that are lexically scoped as auto
variables, i.e., declared inside functions without the static
storage class. The word accessible
is unfortunately a bit imprecise. With static storage class the object could be referenced if its address was leaked out of the function.
The compiler has already prepared storage of the string "Hello, World"
in such a way that it has its own memory address when your application is loaded.
Then when your code s = "Hello, World";
runs, you're copying the memory address of the compiled string into pointer s
. No additional memory has been allocated for the string, and s
itself is a local variable (a pointer) temporarily taking up space on the stack.
s
is on the stack, and ceases to exist when string()
returns. The literal string is stored in the read-only data of the program (this may or may not be the part of the program's code, depending on the compiler and OS); with gcc
, you can make it part of the read-write initialized data by compiling with -fwritable-strings
, but this is a bad idea (it's mostly provided for ancient programs which expect string literals to be writable).
s = "Hello, World";
s
is a local variable whereas "Hello World"
being a string literal is stored in const region of memory and has static storage duration. s
gets destroyed when the function returns but the string literal "Hello World"
doesn't.
try this. may works
#include
static char* str1(void){
static char* s="abc";
return s;
}
int main(int argc,char* argv[]){
printf("%s\n",str1());
return 0;
}
'const' is 'should not be left-valued'.
'static' for a variable ,is 'static allocated'.
(you're returning a static string)
varible s is point to an area which 'static allocated'