where is the string literal allocated ? On the stack ?

Matthew Woehlke mw_triad@users.sourceforge.net
Fri Apr 20 15:44:00 GMT 2007


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>



More information about the Gcc-help mailing list