5

Possible Duplicate:
Is returning a string literal address from a function safe and portable?
“life-time” of string literal in C

Hello i am confused somewhat

char *func()
 {

    return "Hello";
 }

Here "Hello" is sequence/array of characters. It is a local variable and it must vanish away as soon as the function returns. Then how come we are able to get the correct value?

trincot
  • 317,000
  • 35
  • 244
  • 286

4 Answers4

10

The "Hello" is a string literal and will exist for the lifetime of the program. To quote the relevant sections of the C99 standard:

  • 6.4.5 String literals

...The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence...

  • 6.2.4 Storage durations of objects

An object whose identifier is declared with external or internal linkage, or with the storage-class specifier static has static storage duration. Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

The return value of the function should be const char* as an attempt to modify a string literal is undefined behaviour.

hmjd
  • 120,187
  • 20
  • 207
  • 252
  • Are the Strin literals not stored in the stack? Are they allocated from heap? –  Aug 27 '12 at 09:16
  • @GreatCoder They are allocated in read-only memory, likely called `.rodata` or similar linker-gibberish. – Lundin Aug 27 '12 at 09:17
  • @GreatCoder, they are not stored on stack or the heap. They will be compiled directly in the produced binary. I _think_ the region of the binary into which string literals are compiled is named the _data area_. – hmjd Aug 27 '12 at 09:20
  • @hmjd Typically the linker will have one segment `.data` and one segment `.rodata` where the former is for all static storage duration variables (that are not initialized to zero, those are in .bss) and the latter is for read-only variables, ie constants and string literals. – Lundin Aug 27 '12 at 09:23
  • @Lundin, thanks. I was aware of the two sections (initialised and not) but didn't know the names. – hmjd Aug 27 '12 at 09:27
  • One way to think about is to consider a string literal such as `"a"` to be equivalent with there being a `static const char string_a[] = {'a', '\0'};` declaration, and that all uses of `"a"` be replaced with `string_a`, globally, with collisions probably being collapsed, too. – unwind Aug 27 '12 at 10:01
1

It's constant and have constant address in memory.

nshy
  • 1,074
  • 8
  • 13
0

The function destroys the values only after returning the control.

So, By the time the return statement is encountered the "Hello" is placed to return value and then the function destroys the scope;

vivek_jonam
  • 3,237
  • 8
  • 32
  • 44
  • Then it should crash at runtime. question mentions "Then how come we are able to get the correct value" – Neel Basu Aug 27 '12 at 09:11
  • 1
    You are incorrect since a string literal is not a local (automatic) variable and isn't allocated on the stack, but rather as constant, static read-only memory. – Lundin Aug 27 '12 at 09:15
-1

take a look at this : Is returning a string literal address from a function safe and portable?

even if the string were deleted (local variable or dynmic allocation with malloc() and free()) , when you return a pointer, the value can be correct. but, this is an undifined behavior.

Community
  • 1
  • 1
Hicham
  • 983
  • 6
  • 17