On Tue Nov 22, 2005 at 17:53:16 +1100, O Plameras wrote:
>Benno wrote:
>
>>On Tue Nov 22, 2005 at 17:35:23 +1100, Benno wrote:
>> 
>>
>>>On Tue Nov 22, 2005 at 13:51:59 +0800, [EMAIL PROTECTED] wrote:
>>>   
>>>
>>>>Ummmm ...
>>>>
>>>>Coding 3.
>>>>#include <stdio.h>
>>>>#include <stdlib.h>
>>>>#include <string.h>
>>>>char *
>>>>somefunction()
>>>>{
>>>>      char *string2 = "some words";
>>>>      return string2;
>>>>}
>>>>int main (void)
>>>>{
>>>>      char *string;
>>>>      string = somefunction();
>>>>      printf ("\n\nString is: %s\n\n", string);
>>>>      return 0;
>>>>}
>>>>
>>>>somefunction returns string2 which is trash!
>>>>formally: The scope of string2 does not extend to main!
>>>>(even if it works, and it might, it's WRONG)
>>>>     
>>>>
>>>"some words" will be allocated in the .rodata section not on the stack
>>>so it will actually work. (Not that I'd recommend doing this!!).
>>>
>>>I can't find anything in the C spec about return the address of a string 
>>>literal,
>>>it doesn't say wether it is allowed or not.
>>>   
>>>
>>
>>I can't actually find it in the C spec but from googling it does appear
>>that string literals are constants with static storage duration which
>>means they have lifetime of the program.
>>
>>(But I still wouldn't use that style, it is confusing at best.)
>>
>> 
>>
>
>Try compiling my codes with '-g' and run 'gdb'.
>

Umm? And.... ?? What is it meant to show me? I've already said that
a string literal is place in the rodata section in every compiler I've
used, so I know what the implementation does.

But what the implementation does and what the spec says are two widly 
different things.

Benno
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to