safe to return a 'const char *' from a local std::string?

Discussion in 'C++' started by Sims, Jan 27, 2004.

  1. Sims

    Sims Guest

    Hi,

    if i have a code

    const char * GetValue()
    {
    std::string szVectorValue = ...// get a std::string from the vector
    return szVectorValue.c_str();
    }

    Is the code above safe? I think not because the value 'szVectorValue' will
    be released making the const char garbage.

    i could do

    std::string GetValue()
    {
    std::string szVectorValue = ...// get a std::string from the vector
    return szVectorValue;
    }

    but the problem is that i am using sprintf( ... )

    and it would be simpler/clearer if instead of 'GetValue().c_str()' i could
    do 'GetValue()'

    Many thanks for your input.

    Sims
    Sims, Jan 27, 2004
    #1
    1. Advertising

  2. Sims

    void Guest

    U¿ytkownik Sims napisa³, On 2004-01-27 12:12:

    > Hi,
    >
    > if i have a code
    >
    > const char * GetValue()
    > {
    > std::string szVectorValue = ...// get a std::string from the vector
    > return szVectorValue.c_str();
    > }
    >
    > Is the code above safe? I think not because the value 'szVectorValue' will
    > be released making the const char garbage.
    >

    This code isn't safe, szVectorValue is on stack and when you return from
    this function it simply don't exists, so all you get is garbage...

    > i could do
    >
    > std::string GetValue()
    > {
    > std::string szVectorValue = ...// get a std::string from the vector
    > return szVectorValue;
    > }
    >
    > but the problem is that i am using sprintf( ... )
    >
    > and it would be simpler/clearer if instead of 'GetValue().c_str()' i could
    > do 'GetValue()'
    >

    If you don't want 'GetValue().c_str()' you could try something like this

    const char * GetValue()
    {
    static std::string szVectorValue ;
    szVectorValue = ...// get a std::string from the vector
    return szVectorValue.c_str();
    }
    but I'm not sure it is better solution than 'GetValue().c_str()'.
    Best regards
    Darek Ostolski
    --
    ld: bad magic number
    void, Jan 27, 2004
    #2
    1. Advertising

  3. Sims

    Sims Guest

    > > std::string GetValue()
    > > {
    > > std::string szVectorValue = ...// get a std::string from the

    vector
    > > return szVectorValue;
    > > }
    > >
    > > but the problem is that i am using sprintf( ... )
    > >
    > > and it would be simpler/clearer if instead of 'GetValue().c_str()' i

    could
    > > do 'GetValue()'
    > >

    > If you don't want 'GetValue().c_str()' you could try something like this
    >
    > const char * GetValue()
    > {
    > static std::string szVectorValue ;
    > szVectorValue = ...// get a std::string from the vector
    > return szVectorValue.c_str();
    > }


    Thanks.

    But that's also on the stack so will also become garbage, won't it?

    Sims
    Sims, Jan 27, 2004
    #3
  4. Sims

    void Guest

    U¿ytkownik Sims napisa³, On 2004-01-27 12:25:

    >>const char * GetValue()
    >>{
    >> static std::string szVectorValue ;
    >> szVectorValue = ...// get a std::string from the vector
    >> return szVectorValue.c_str();
    >>}

    >
    >
    > Thanks.
    >
    > But that's also on the stack so will also become garbage, won't it?
    >
    > Sims
    >
    >


    No it's static, so it isn't on stack, and you don't get garbage.
    This static string is really bad idea. You have one szVectorValue in
    your program, so if szVectorValue need to reallocate memory, you will
    have garbage previously returned. This also means that if you call
    GetValue two times, previously returned value will be 'magicly'
    replaced with last returned value. Is is better idea to return copy of
    std::string and call it by 'GetValue().c_str()'.

    Best regards
    Darek Ostolski
    --
    Windows: A thirty-two bit extension to a sixteen-bit patch to a
    eight-bit operating system originally coded for a four-bit
    microprocessor written by a two-bit company that can't stand one bit of
    competition. [Jargon File 4.3.3]
    void, Jan 27, 2004
    #4
  5. Sims wrote:
    >> >

    >> If you don't want 'GetValue().c_str()' you could try something like this
    >>
    >> const char * GetValue()
    >> {
    >> static std::string szVectorValue ;
    >> szVectorValue = ...// get a std::string from the vector
    >> return szVectorValue.c_str();
    >> }

    >
    > Thanks.
    >
    > But that's also on the stack so will also become garbage, won't it?
    > ...


    No, it's 'static' which means that it is not on stack. However, in
    general case writing non-reentrant functions is a bad idea unless you
    have a good reason to do it. Personally, I wouldn't consider the above
    problem to be a good reason for this type of trick.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 27, 2004
    #5
  6. Sims

    Sims Guest

    > >
    > > But that's also on the stack so will also become garbage, won't it?
    > > ...

    >
    > No, it's 'static' which means that it is not on stack. However, in
    > general case writing non-reentrant functions is a bad idea unless you
    > have a good reason to do it. Personally, I wouldn't consider the above
    > problem to be a good reason for this type of trick.
    >


    So do you also suggest i do 'GetValue().c_str()' rather than try to be lazy?

    Sims
    Sims, Jan 27, 2004
    #6
  7. Sims wrote:
    >> >
    >> > But that's also on the stack so will also become garbage, won't it?
    >> > ...

    >>
    >> No, it's 'static' which means that it is not on stack. However, in
    >> general case writing non-reentrant functions is a bad idea unless you
    >> have a good reason to do it. Personally, I wouldn't consider the above
    >> problem to be a good reason for this type of trick.
    >>

    >
    > So do you also suggest i do 'GetValue().c_str()' rather than try to be lazy?
    > ...


    Yes. Using C standard library functions with C++ standard library
    classes is supposed to be cumbersome, since this is not exactly a
    natural combination :)

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Jan 27, 2004
    #7
  8. Sims

    Jorge Rivera Guest

    Sims wrote:

    > i could do
    >
    > std::string GetValue()
    > {
    > std::string szVectorValue = ...// get a std::string from the vector
    > return szVectorValue;
    > }
    >
    > but the problem is that i am using sprintf( ... )



    Do you really need to use sprintf??? You can use ostringstream to do
    string formatting, and you can forget about your .c_str() problems.

    Just a though, I have no idea what you are trying to accomplish.

    Jorge L
    Jorge Rivera, Jan 28, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    5
    Views:
    2,372
    tom_usenet
    Aug 2, 2004
  2. Replies:
    24
    Views:
    814
    Netocrat
    Oct 30, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,013
    Ian Collins
    May 9, 2006
  4. Replies:
    2
    Views:
    8,995
    Heinz Ozwirk
    Aug 9, 2006
  5. Javier
    Replies:
    2
    Views:
    547
    James Kanze
    Sep 4, 2007
Loading...

Share This Page