0

I know that string literal used in program gets storage in read only area for eg.

//global
const char *s="Hello World \n";

Here string literal "Hello World\n" gets storage in read only area of program . Now suppose I declare some literal in body of some function like

func1(char *name)
{
    const char *s="Hello World\n";
}

As local variables to function are stored on activation record of that function, is this the same case for string literals also? Again assume I call func1 from some function func2 as

func2()
{
    //code
    char *s="Mary\n";

    //call1
    func1(s);

    //call2
    func1("Charles");

    //code
}

Here above,in 1st call of func1 from func2, starting address of 's' is passed i.e. address of s[0], while in 2nd call I am not sure what does actually happens. Where does string literal "Charles" get storage. Whether some temperory is created by compiler and it's address is passed or something else happens? I found literals get storage in "read-only-data" section from String literals: Where do they go? but I am unclear about whether that happens only for global literals or for literals local to some function also. Any insight will be appreciable. Thank you.

Community
  • 1
  • 1
chammu
  • 1,275
  • 1
  • 18
  • 26
  • I think all static memory-based values get stored in the read-only area, but to be sure of where it is actually stored you should step through the code with a debugger and/or open the executable with a hex editor. – abiessu Oct 29 '13 at 19:38
  • 1
    Try compiling your .c file like this "gcc -S -c file.c -o file.s" and look at file.s. Down at the bottom you will see a "text" section that contains all your strings. If you look carefully at the assembly code you can see how they are loaded. – Charlie Burns Oct 29 '13 at 19:39
  • While this is true for `gcc`, another interesting question would be whether there is any requirement for this by any standard. – s.bandara Oct 29 '13 at 19:40

2 Answers2

4

A C string literal represents an array object of type char[len+1], where len is the length, plus 1 for the terminating '\0'. This array object has static storage duration, meaning that it exists for the entire execution of the program. This applies regardless of where the string literal appears.

The literal itself is an expression type char[len+1]. (In most but not all contexts, it will be implicitly converted to a char* value pointing to the first character.)

Compilers may optimize this by, for example, storing identical string literals just once, or by not storing them at all if they're never referenced.

If you write this:

const char *s="Hello World\n";

inside a function, the literal's meaning is as I described above. The pointer object s is initialized to point to the first character of the array object.

For historical reasons, string literals are not const in C, but attempting to modify the corresponding array object has undefined behavior. Declaring the pointer const, as you've done here, is not required, but it's an excellent idea.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • got it. that means literals appearing in function call also get static storage for entire execution of program. This seems to increase needed static storage size as we could have used many literals in many different functions and function calls inspite of the fact that many function may not be always used(called at runtime). – chammu Oct 29 '13 at 20:27
1

Where string literals (or rather, the character arrays they are compiled to) are located in memory is an implementation detail in the compiler, so if you're thinking about what the C standard guarantees, they could be in a number of places, and string literals used in different ways in the program could end up in different places.

But in practice most compilers will treat all string literals the same, and they will probably all end up in a read-only segment. So string literals used as function arguments, or used inside functions, will be stored in the same place as the "global" ones.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
  • does that means string literals local to function also end up in read-only text segments? – chammu Oct 29 '13 at 19:55
  • Your "answer" doesn't answer the question the OP asked, only your subsequent comment does. In fact, your answer is incorrect; string literals cannot "be anywhere", specifically they cannot be on the stack, because they have static storage duration. – Jim Balter Oct 29 '13 at 20:05
  • @Jim Balter: Yes, "anywhere" is wrong, and should perhaps be changed to something like "in a variety of places, and still conform to the standard". I thought that "will treat all string literals the same" answered the question. But I should probably clarify. – Thomas Padron-McCarthy Oct 29 '13 at 20:14
  • @Jim Balter I failed to find in the C spec that string literals cannot be on the stack. Could not even find "stack" in the C spec anywhere. Certainly string literals needed to have static storage duration, but how that is accomplished appears to implementation defined. – chux - Reinstate Monica Oct 29 '13 at 21:37
  • @chux That they have static storage duration **implies** that they aren't kept in function stack frames -- which are an implementation mechanism, so they don't appear in the standard. – Jim Balter Nov 01 '13 at 00:48