Does c_str property of string class allocate memory?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alfonso Morra

    Does c_str property of string class allocate memory?

    Hi,

    Doest the c_str() method of the string class allocate memory. if it does
    alloc mem behind the scenes - whose responsibility is it to cleanup
    after (i.e. free the returned char*?)

    Tkx

  • peter.koch.larsen@gmail.com

    #2
    Re: Does c_str property of string class allocate memory?


    Alfonso Morra wrote:[color=blue]
    > Hi,
    >
    > Doest the c_str() method of the string class allocate memory. if it does
    > alloc mem behind the scenes - whose responsibility is it to cleanup
    > after (i.e. free the returned char*?)
    >
    > Tkx[/color]
    This is not specified in the standard, so the answer is "could be", but
    I haven't yet met a string which did allocate.
    In all cases you are not responsible for the deallocation.

    /Peter

    Comment

    • Robbie Hatley

      #3
      Re: Does c_str property of string class allocate memory?

      "Alfonso Morra" <sweet-science@the-ring.com> wrote in message news:dharad$o8g $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
      > Hi,
      >
      > Doest the c_str() method of the string class allocate memory. if it does
      > alloc mem behind the scenes - whose responsibility is it to cleanup
      > after (i.e. free the returned char*?)
      >
      > Tkx[/color]

      That's up to the compiler author.

      You don't need to worry about allocation or deallocation;
      that's all handled for you automatically.

      Some warnings about c_str() :

      If you do something like this:

      std::string Catz = "Round and round she goes!";
      const char* Batz = Catz.c_str();

      Then Batz is a pointer to an object which is owned by the
      std::string object Catz. This means you need to remember
      these two things:

      1. You can NOT write to the location pointed to by Batz!
      It's read-only. In fact, a good compiler won't even
      let you omit the "const" in the definition of Batz above.

      2. If Catz goes out of scope, Batz is no-longer guaranteed
      to point to anything useful. Dereferencing Batz at that
      point might crash your program (or your system).


      Cheers,
      Robbie Hatley
      Tustin, CA, USA
      email: lonewolfintj at pacbell dot net
      web: home dot pacbell dot net slant earnur slant


      Comment

      • Alfonso Morra

        #4
        Re: Does c_str property of string class allocate memory?



        Robbie Hatley wrote:[color=blue]
        > "Alfonso Morra" <sweet-science@the-ring.com> wrote in message news:dharad$o8g $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...
        >[color=green]
        >>Hi,
        >>
        >>Doest the c_str() method of the string class allocate memory. if it does
        >> alloc mem behind the scenes - whose responsibility is it to cleanup
        >>after (i.e. free the returned char*?)
        >>
        >>Tkx[/color]
        >
        >
        > That's up to the compiler author.
        >
        > You don't need to worry about allocation or deallocation;
        > that's all handled for you automatically.
        >
        > Some warnings about c_str() :
        >
        > If you do something like this:
        >
        > std::string Catz = "Round and round she goes!";
        > const char* Batz = Catz.c_str();
        >
        > Then Batz is a pointer to an object which is owned by the
        > std::string object Catz. This means you need to remember
        > these two things:
        >
        > 1. You can NOT write to the location pointed to by Batz!
        > It's read-only. In fact, a good compiler won't even
        > let you omit the "const" in the definition of Batz above.
        >
        > 2. If Catz goes out of scope, Batz is no-longer guaranteed
        > to point to anything useful. Dereferencing Batz at that
        > point might crash your program (or your system).
        >
        >
        > Cheers,
        > Robbie Hatley
        > Tustin, CA, USA
        > email: lonewolfintj at pacbell dot net
        > web: home dot pacbell dot net slant earnur slant
        >
        >[/color]
        tkx

        Comment

        • Default User

          #5
          Re: Does c_str property of string class allocate memory?

          Robbie Hatley wrote:

          [color=blue]
          > Some warnings about c_str() :
          >
          > If you do something like this:
          >
          > std::string Catz = "Round and round she goes!";
          > const char* Batz = Catz.c_str();
          >
          > Then Batz is a pointer to an object which is owned by the
          > std::string object Catz. This means you need to remember
          > these two things:
          >
          > 1. You can NOT write to the location pointed to by Batz!
          > It's read-only. In fact, a good compiler won't even
          > let you omit the "const" in the definition of Batz above.
          >
          > 2. If Catz goes out of scope, Batz is no-longer guaranteed
          > to point to anything useful. Dereferencing Batz at that
          > point might crash your program (or your system).[/color]

          Worse than that, if there's any call to a non-const member function of
          Catz, then Batz is no longer valid. It's a poor idea to EVER try to
          keep the pointer returned. You should always get a fresh copy before
          using it.




          Brian

          Comment

          • Mike Wahler

            #6
            Re: Does c_str property of string class allocate memory?


            "Alfonso Morra" <sweet-science@the-ring.com> wrote in message
            news:dharad$o8g $1@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
            > Hi,
            >
            > Doest the c_str() method of the string class allocate memory.[/color]

            Maybe, maybe not.
            [color=blue]
            > if it does alloc mem behind the scenes - whose responsibility is it to
            > cleanup after (i.e. free the returned char*?)[/color]

            The std::string class handles all its own memory management,
            as do all the other standard library types.

            -Mike


            Comment

            Working...