lifetime of temporary object from function return & optimization

Discussion in 'C++' started by pt, May 30, 2005.

  1. pt

    pt Guest

    Hallo,

    i wonder how it is going to be of this code below regarding of the
    return of temporary object.

    Prototypes:
    ===========

    bool Activation(TCHAR *c);
    std::basic_string<TCHAR> GetFile();

    Func Call:
    ==========

    Activation((TCHAR *) myObj.GetFile().c_str());


    Summary of the Question:
    ========================
    It works fine under EVC4 & MSVC6. However, I dont know if it is
    portable to g++
    or other compiler.

    - Is there any answer to the lifetime of temporary object for
    all compilers? Does it also work fine under other compilers?

    - In cosidering of effiency of the code. Is there any better solution
    to just create a temporary obj like this ?

    TCHAR* temp = myObj.GetFile().c_str();
    Activation(temp);

    - What is the lifetime of the return value from myObj.GetFile().c_str()
    before Activation(..) is called?


    Thank you very much for any suggestion.

    regards,
    pattreeya


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    pt, May 30, 2005
    #1
    1. Advertising

  2. pt wrote:
    > i wonder how it is going to be of this code below regarding of the
    > return of temporary object.
    >
    > Prototypes:
    > ===========
    >
    > bool Activation(TCHAR *c);
    > std::basic_string<TCHAR> GetFile();
    >
    > Func Call:
    > ==========
    >
    > Activation((TCHAR *) myObj.GetFile().c_str());


    This is a VERY BAD IDEA(tm). "c_str()" returns a pointer to a _const_
    TCHAR, casting away constness like this is utterly dangerous.

    > Summary of the Question:
    > ========================
    > It works fine under EVC4 & MSVC6.


    "Works" is a very subjective statement, isn't it?

    > However, I dont know if it is
    > portable to g++
    > or other compiler.


    It looks portable to me, but since I don't know what 'Activation' does
    to the pointer passed to it, there is no way to tell if there are any
    ill effects (like undefined behaviour).

    > - Is there any answer to the lifetime of temporary object for
    > all compilers? Does it also work fine under other compilers?


    A temporary is destroyed as the last step of evaluating the full
    expression during evaluation of which the temporary was created. In
    your case, since 'GetFile' returns an object, and that function call
    is a sub-expression of an expression used to initialise the argument
    of the 'Activation' function call, the temporary should survive until
    'Activation' returns.

    Compilers are many, some of them implement the Standard requirements
    closer to the ideal than others. The lifetime of a temporary is one
    of the issues that most compilers implement correctly, AFAICT.

    > - In cosidering of effiency of the code. Is there any better solution
    > to just create a temporary obj like this ?
    >
    > TCHAR* temp = myObj.GetFile().c_str();


    That should not compile. 'c_str()' returns 'TCHAR const*' and you are
    not allowed to convert it to 'TCHAR*' without a const_cast. And I urge
    you not to, anyway.

    > Activation(temp);


    No, this is definitely NOT going to work. 'temp' is a dangling pointer.
    The temporary returned by 'GetFile' will be disposed of at the end of
    initialising 'temp', which will *immediately* make it invalid.

    > - What is the lifetime of the return value from
    > myObj.GetFile().c_str() before Activation(..) is called?


    'c_str()' does not create a separate temporary to speak of. It does
    however, create a pointer to the data in the other temporary, the
    'basic_string<TCHAR>' object. How it does that is implemenation-
    defined. The pointer remains valid until the next call to a non-const
    member function for the same object (destructor is included).

    V
    Victor Bazarov, May 30, 2005
    #2
    1. Advertising

  3. Victor Bazarov wrote:
    > pt wrote:
    >> i wonder how it is going to be of this code below regarding of the
    >>return of temporary object.
    >>


    [snip]

    >
    > V
    >
    >


    Ok, I give up.

    What is a "TCHAR"?

    Is code using "TCHAR" platform-portable?

    Larry
    Larry I Smith, May 30, 2005
    #3
  4. pt

    Alan Johnson Guest

    Larry I Smith wrote:
    > Victor Bazarov wrote:
    >
    >>pt wrote:
    >>
    >>> i wonder how it is going to be of this code below regarding of the
    >>>return of temporary object.
    >>>

    >
    >
    > [snip]
    >
    >
    >>V
    >>
    >>

    >
    >
    > Ok, I give up.
    >
    > What is a "TCHAR"?
    >
    > Is code using "TCHAR" platform-portable?
    >
    > Larry


    It is a preprocessor macro defined in the Win32 API headers (i.e.,
    non-standard, non-portable) that evalutes to either char or wchar
    depending on whether UNICODE is defined. Nearly all of the Win32 API
    functions that take strings as parameters are actually preprocessor
    macros that evaluate to a FunctionNameA or FunctionNameW version, which
    take char or wchar parameters, respectively.
    Alan Johnson, May 30, 2005
    #4
  5. Larry I Smith wrote:
    > Victor Bazarov wrote:
    >> pt wrote:
    >>> i wonder how it is going to be of this code below regarding of the
    >>> return of temporary object.
    >>>

    >
    > [snip]
    >
    >>
    >> V
    >>
    >>

    >
    > Ok, I give up.
    >
    > What is a "TCHAR"?


    It matters not to the question at hand.

    > Is code using "TCHAR" platform-portable?


    As much as you want to make it. I presume somewhere in the OP's code
    there is a line like

    typedef wchar_t TCHAR;
    or
    typedef char TCHAR;

    depending on some other circumstances (macros/OS settings/whatever).

    V
    Victor Bazarov, May 31, 2005
    #5
  6. Victor Bazarov wrote:
    > Larry I Smith wrote:
    >>Victor Bazarov wrote:
    >>>pt wrote:
    >>>> i wonder how it is going to be of this code below regarding of the
    >>>>return of temporary object.
    >>>>

    >>[snip]
    >>
    >>>V
    >>>
    >>>

    >>Ok, I give up.
    >>
    >>What is a "TCHAR"?

    >
    > It matters not to the question at hand.
    >
    >>Is code using "TCHAR" platform-portable?

    >
    > As much as you want to make it. I presume somewhere in the OP's code
    > there is a line like
    >
    > typedef wchar_t TCHAR;
    > or
    > typedef char TCHAR;
    >
    > depending on some other circumstances (macros/OS settings/whatever).
    >
    > V
    >
    >


    Ok, Alan answered it in his post - It's WIN32 API specific.

    So, code using TCHAR is not portable.

    Thanks Alan.

    Regards,
    Larry
    Larry I Smith, May 31, 2005
    #6
  7. pt

    pt Guest

    Thanks a lot for all of the comments.

    in the header file,typedef char TCHAR; so it should be portable ...

    Well, regarding the temporary object, can we say that its lifetime of
    return of GetFile() is existing until the end of call of
    Activation(..), so to say at the end of statement?
    pt, May 31, 2005
    #7
  8. pt

    Guest

    Hi,
    The return value of std::basic_string<TCHAR>::c_str() is valid until
    you call non-constant member function of this object.


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    , May 31, 2005
    #8
  9. pt

    Kodt Guest

    A temporary object lives till the end of whole expression.
    Thus,
    Activation( myObj.GetFile().c_str() );
    is correct, while
    char* temp = myObj.GetFile().c_str();
    will be invalidated immediately after ';' and should not be used in
    Activation(temp);

    By the way,

    If you have to cast to (TCHAR*) due to removing constness, please
    verify that you don't modify the paramenter string in Activation(). If
    no, you'd better declare it as
    bool Activation(const TCHAR *c);
    or (same as above)
    bool Activation(LPCTSTR c);

    Also note that, if TCHAR != char (being compiled for Unicode), c-style
    casting from const char* -> const TCHAR* is meaningless and harmful.
    You should use
    either basic_string<TCHAR>
    or convert types with special purpose macros - A2T (see
    http://msdn.microsoft.com/library/en-us/vclib/html/_atl_String_Conversion_Macros.asp
    for details)


    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]
    Kodt, Jun 1, 2005
    #9
    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. Replies:
    7
    Views:
    3,212
    James Kanze
    Feb 12, 2008
  2. Replies:
    5
    Views:
    389
    James Kanze
    Jun 13, 2008
  3. Juha Nieminen
    Replies:
    2
    Views:
    272
  4. Igor R.
    Replies:
    2
    Views:
    421
    James Kanze
    Jan 28, 2009
  5. Daniel
    Replies:
    2
    Views:
    206
    Daniel
    Jun 11, 2013
Loading...

Share This Page