lifetime of char[]?

Discussion in 'C++' started by Stefan Ram, Nov 9, 2013.

  1. Stefan Ram

    Stefan Ram Guest

    In the following example:

    { const char * concat =( stdstring + stdstring_ ).c_str(); ... }

    it seems that the value of c_str() is a pointer to a
    temporary char array. Now, that POINTER is used to
    initialize the variable »concat«.

    There might be a rule that a temporary can be used to
    initialize an object with automatic storage duration, and
    then the lifetime of that temporary is extended to the
    lifetime of that automatic object.

    But what about the temporary char ARRAY pointed to by the
    pointer »concat«? Is there a rule that extends its lifetime
    too? After all, it is not the same as the POINTER to it,
    so another rule would be needed to extend its lifetime too.

    So, can »concat« safely (portably) be dereferenced in this
    block to get access to the concatenation of the C++-strings
    as a C-string?
     
    Stefan Ram, Nov 9, 2013
    #1
    1. Advertising

  2. Stefan Ram

    Melzzzzz Guest

    On 9 Nov 2013 01:18:25 GMT
    -berlin.de (Stefan Ram) wrote:

    > In the following example:
    >
    > { const char * concat =( stdstring + stdstring_ ).c_str(); ... }
    >
    >
    > So, can »concat« safely (portably) be dereferenced in this
    > block to get access to the concatenation of the C++-strings
    > as a C-string?
    >


    No.

    --
    drwxr-xr-x 2 bmaxa bmaxa 4096 Nov 7 02:00 .
     
    Melzzzzz, Nov 9, 2013
    #2
    1. Advertising

  3. Stefan Ram

    Öö Tiib Guest

    On Saturday, 9 November 2013 03:18:25 UTC+2, Stefan Ram wrote:
    > In the following example:
    >
    > { const char * concat =( stdstring + stdstring_ ).c_str(); ... }
    >
    > it seems that the value of c_str() is a pointer to a
    > temporary char array. Now, that POINTER is used to
    > initialize the variable »concat«.


    Yes, and pointlessly so. Smart enough tool could warn. It is
    useless pointer on very next line. That works:

    {
    std::string& ref =( stdstring + stdstring_ );
    const char* concat = ref.c_str();
    // ...
    } // <- ref is valid until here and so is concat

    But generally ... avoid raw pointers.
     
    Öö Tiib, Nov 9, 2013
    #3
  4. On 09.11.2013 10:24, Öö Tiib wrote:
    > On Saturday, 9 November 2013 03:18:25 UTC+2, Stefan Ram wrote:
    >> In the following example:
    >>
    >> { const char * concat =( stdstring + stdstring_ ).c_str(); ... }
    >>
    >> it seems that the value of c_str() is a pointer to a
    >> temporary char array. Now, that POINTER is used to
    >> initialize the variable »concat«.

    >
    > Yes, and pointlessly so. Smart enough tool could warn. It is
    > useless pointer on very next line. That works:
    >
    > {
    > std::string& ref =( stdstring + stdstring_ );
    > const char* concat = ref.c_str();
    > // ...
    > } // <- ref is valid until here and so is concat
    >
    > But generally ... avoid raw pointers.


    The above won't compile.

    But

    "std::string const&"

    or

    "std::string&&"

    would.

    The easiest and safest solution is to store the concatenation result in
    a std::string, since that's the string class used in the code.


    Cheers,

    - Alf (nitpicking)
     
    Alf P. Steinbach, Nov 9, 2013
    #4
  5. Stefan Ram

    Öö Tiib Guest

    On Saturday, 9 November 2013 13:41:12 UTC+2, Leigh Johnston wrote:
    > On 09/11/2013 09:24, Öö Tiib wrote:
    > > On Saturday, 9 November 2013 03:18:25 UTC+2, Stefan Ram wrote:
    > >> In the following example:
    > >>
    > >> { const char * concat =( stdstring + stdstring_ ).c_str(); ... }
    > >>
    > >> it seems that the value of c_str() is a pointer to a
    > >> temporary char array. Now, that POINTER is used to
    > >> initialize the variable »concat«.

    > >
    > > Yes, and pointlessly so. Smart enough tool could warn. It is
    > > useless pointer on very next line. That works:
    > >
    > > {
    > > std::string& ref =( stdstring + stdstring_ );
    > > const char* concat = ref.c_str();
    > > // ...
    > > } // <- ref is valid until here and so is concat

    >
    > That is wrong Mr Homophobic Bigot; it needs to be reference to const.


    Thanks for correcting, Mr Foulmouthed Fagot, that was a typo. OP
    needed const but I mistyped it somehow.

    > > But generally ... avoid raw pointers.

    >
    > I have to agree Mr Homophobic Bigot.
    >
    > /Leigh
     
    Öö Tiib, Nov 9, 2013
    #5
    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. wwj
    Replies:
    7
    Views:
    561
  2. wwj
    Replies:
    24
    Views:
    2,525
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    482
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    592
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,071
    Ian Collins
    May 9, 2006
Loading...

Share This Page