3

Recently came across following code which declares a char *p, assigns value such as p="GOOD" and returns return p. Is the return value valid when function call is completed?

const char * get_state(int state)
{
    char *p;

    if (state) {
        p = "GOOD";
    }
    else
    {
        p = "BAD";
    }

    return p;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
pkumarn
  • 1,383
  • 4
  • 22
  • 29

3 Answers3

6

Yes, it is valid. The string literals "GOOD" and "BAD" are guaranteed to be in permanent, static storage, and will continue to exist even after get_state() returns.

// Invalid
char str[] = "GOOD";
char *p = str;
return p;

// Valid
char *p = "GOOD";
return p;
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • `main(){ char *ptr = get_state(1); }` Now Ptr will have 4000 which already created in stack memory in get_state method. Is it correct way to return in get_state method ? – Mani Apr 10 '13 at 13:42
  • @mani I'm not following. What is this number 4000? The question deals with the string literals `"GOOD"` and `"BAD"`. I don't know what 4000 is... – John Kugelman Apr 10 '13 at 13:45
  • It's value of `p` .that is *p . I take 4000 as created memory(example only).. – Mani Apr 10 '13 at 13:50
  • 1
    @mani - I think you're missing the point here. There is a chunk of memory in the program dedicated to permanent storage (it's not "stack memory") it's built into your binary. String literals go there, the C language (see my answer for reference) guarantees that what you put into this permanent static storage location is valid for the lifetime of your program. So yes, it's valid to return `p` when `char *p = "STRING LITERAL";` – Mike Apr 10 '13 at 13:59
  • @Mike now I clear with static storage location . Cheers :) – Mani Apr 10 '13 at 14:03
3

In this specific case the return value is good because it points to static storage (the memory where the string literals are is allocated for the duration of the process).

If the pointer returned was pointing to a local variable then there would be a problem, e.g.:

// BAD! You cannot use the return value of this function!
const char * get_state(int state)
{
    char c;
    return &c;
}
Jon
  • 428,835
  • 81
  • 738
  • 806
1
  • Is the return value valid when function call is completed?

Yes, because p points at a string literal, a string literal has static storage duration and that means:

(Section 6.2.4 p3 of the C spec)

Its lifetime is the entire execution of the program and its stored value is initialized only once, prior to program startup.

So the C language guarantees that the strings "GOOD" and "BAD" will be available everywhere in your program and get_state() will just be returning a pointer to one of those memory locations.

Mike
  • 47,263
  • 29
  • 113
  • 177
  • Thanks for all comments. One more clarification: as I know 'p' should hold the address of another variable , is it OK to assign a string literal? – pkumarn Apr 11 '13 at 15:28
  • @pkumarn - Yes, it's fine. When you do something like `p="GOOD";` you're assigning the *address* of the string literal to `p`. The only requirement `p` has is that it needs to hold the address of some sort of `char *` the string literal is `const char *` so it matches, thus `p` is good to point there. – Mike Apr 11 '13 at 15:37