CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2002
    Posts
    78

    Returning pointer to string literal?

    Is the following valid?

    Code:
       const char* someFunction( void )
       {
          return "some string";
       }

    Isn't the string created on the stack, and thus evaporates when the function exits, and thus the pointer is invalid? Wouldn't it be wrong to rely on it being created in some theoretcical "read only" space where it has functionally static time scope?

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Returning pointer to string literal?

    Quote Originally Posted by Gorgor
    Is the following valid?

    Code:
        const char* someFunction( void )
        {
           return "some string";
        }

    Isn't the string created on the stack, and thus evaporates when the function exits, and thus the pointer is invalid? Wouldn't it be wrong to rely on it being created in some theoretcical "read only" space where it has functionally static time scope?
    It is valid, because the string is not created on your function stack.

    It belongs to the Application Stack.

    Read a similar thread here.

  3. #3
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Returning pointer to string literal?

    Quote Originally Posted by ANSI 96 , 2.13.4.1
    An ordinary string literal has type "array of n const
    char" and static storage duration (_basic.stc_), where n is the size
    of the string as defined below, and is initialized with the given
    characters.
    ____________________________
    "Programs must be written for people to read, and only incidentally for machines to execute."

  4. #4
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Returning pointer to string literal?

    I think it is wrong in this manner:

    A literal is a char[] not a char *, so your returning the address of a literal, not a real pointer.

    This code fails on MSVC 6.0:
    Code:
    const char* someFunction( void )
    {
       const char cmsg[16] = "Some String\n";
       return cmsg;
    }
    as it should, because cmsg goes out of scope....

    But frighteningly enough this code does not:
    Code:
    const char* someFunction( void )
    {
       const char *cmsg = "Some String\n";
       return cmsg;//"some string";
    }
    Neither does this code:
    Code:
    const char* someFunction( void )
    {
       const char cmsg[16] = "Some String\n";
       return "some string";
    }
    But you have to keep in mind, returning a literal string is the prob the least progressive coding style on the planet.

    ahoodin

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Returning pointer to string literal?

    Quote Originally Posted by ahoodin
    A literal is a char[] not a char *, so your returning the address of a literal, not a real pointer.
    A pointer is an address...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Returning pointer to string literal?

    MSVC 6.0 is acting correctly here.

    Code:
    const char* someFunction( void )
    {
       const char cmsg[16] = "Some String\n";
       return cmsg;
    }
    When you declare your array that way it is local, and it initialises the array by copying the string into it. That you have declared it as const is relevant only in that you cannot modify it within the scope of the function. You are therefore returning the address of a local variable.

    Code:
    const char* someFunction( void )
    {
       const char *cmsg = "Some String\n";
       return cmsg;//"some string";
    }
    This is different now as it does not copy the literal anywhere but simply gets cmsg to point to its location in the static application space. When it returns the pointer, it is still pointing to the same memory and is still valid.

    Code:
    const char* someFunction( void )
    {
       const char cmsg[16] = "Some String\n";
       return "Some String\n";
    }
    Even with me now modifying your return to make it return exactly the same string as the line above, this is OK. cmsg goes out of scope but that is not what your function is returning. Your compiler may decide to create two different strings with the value "Some String\n" or it may be clever and create only one copy, but either way, your function will actually return a pointer to where it decides to store the second one (which may or may not be the first).

  7. #7
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Returning pointer to string literal?

    Conceptually you are right, however syntatically you are wrong. Otherwise I could write this:
    Code:
    *&0x0041f01c = 100;
    and not get this:
    Code:
    C:\projects\literaltest\literaltest.cpp(11) : error C2101: '&' on constant
    C:\projects\literaltest\literaltest.cpp(11) : error C2100: illegal indirection
    C:\projects\literaltest\literaltest.cpp(11) : error C2106: '=' : left operand must be l-value
    A pointer is a variable that contains a memory address.

    It can be dereferenced with the asterisk.

    ahoodin

  8. #8
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Returning pointer to string literal?

    Quote Originally Posted by NMTop40
    MSVC 6.0 is acting correctly here.

    Code:
    const char* someFunction( void )
    {
       const char cmsg[16] = "Some String\n";
       return cmsg;
    }
    When you declare your array that way it is local, and it initialises the array by copying the string into it. That you have declared it as const is relevant only in that you cannot modify it within the scope of the function. You are therefore returning the address of a local variable.

    Code:
    const char* someFunction( void )
    {
       const char *cmsg = "Some String\n";
       return cmsg;//"some string";
    }
    This is different now as it does not copy the literal anywhere but simply gets cmsg to point to its location in the static application space. When it returns the pointer, it is still pointing to the same memory and is still valid.

    Code:
    const char* someFunction( void )
    {
       const char cmsg[16] = "Some String\n";
       return "Some String\n";
    }
    Even with me now modifying your return to make it return exactly the same string as the line above, this is OK. cmsg goes out of scope but that is not what your function is returning. Your compiler may decide to create two different strings with the value "Some String\n" or it may be clever and create only one copy, but either way, your function will actually return a pointer to where it decides to store the second one (which may or may not be the first).
    I see so the function actually returns the pointer.

    ahoodin

  9. #9
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Returning pointer to string literal?

    Quote Originally Posted by ahoodin
    Conceptually you are right, however syntatically you are wrong. Otherwise I could write this:
    Code:
    *&0x0041f01c = 100;
    Note that your counterexample is incorrect: 0x0041f01c is neither pointer nor address: it's just an integer. However, you can do the same using correct type:
    Code:
    *((int*)(void*)0x0041f01c) = 100;
    "Programs must be written for people to read, and only incidentally for machines to execute."

  10. #10
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Returning pointer to string literal?

    But your casting it to be a pointer, which makes you cheating!

    ahoodin

  11. #11
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Returning pointer to string literal?

    This would be illegal (I assume)

    Code:
    const int * x = &100;
    This would be legal:

    Code:
    const int * x = { 100 };
    And the {100} would be stored in a similar way to strings. In fact a string literal "String\n" is no more than a short-hand for
    Code:
    { 'S', 't', 'r', 'i', 'n', 'g', '\n', '\0' }
    This would return an invalid pointer:

    Code:
    const int * a()
    {
       const int x = 100;
       return &x;
    }
    This would return a valid pointer (I presume)
    Code:
    const int * a()
    {
       const int * x = { 100 };
       return x;
    }
    So would this:
    Code:
    const int * a()
    {
       return {100};
    }
    This would fail (would compile but return auto-pointer)
    Code:
    const int * a()
    {
       const int x[] = {100};
       return x;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured