Re: where is the string literal allocated ? On the stack ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From C99 , 6.4.5,

"5. In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. ..."



Therefore, the string literal must be stored on a "static storage", ie. it is invalid to put it on stack.



Mike





----- Original Message -----
From: "Matthew Woehlke" <mw_triad@xxxxxxxxxxxxxxxxxxxxx>
To: <gcc-help@xxxxxxxxxxx>
Sent: Friday, April 20, 2007 10:36 AM
Subject: Re: where is the string literal allocated ? On the stack ?


Michael Gong wrote:
Though it's not related with gcc, could anyone help me with following question:

Where is the string literal allocated ? Is it on the stack ?

For example, where is "abc" allocated ?

char * foo() {
   return "abc";
}

It can't be on the stack in this case, because if it was, it would cease to exist when foo() returns.

AFAIK string literals all live in the read-only data segment (.text?).

(Your prototype is wrong, btw; you are casting a const char * that lives in read-only memory to a char *, which is just *ASKING* for your program to SEGV at some point.)

--
Matthew
<insert bad pun... on second thought, better not>




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux