question about std::string/map

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

    question about std::string/map

    Hello,

    I have:

    std::map<std::s tring, std::string> m;
    std::string s1;
    char *p = "hello world", *p1 = "value";
    (a) s1 = p;

    is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
    string now?

    (b) m[p] = p1;

    Is that okay? will that create a valid entry into the map?
    Or should I use: map[s1] = "value"; ?

    Btw, can someone tell me about std::string and its reference count issues?

    --
    Elias


  • Sharad Kala

    #2
    Re: question about std::string/map


    "lallous" <lallous@lgwm.o rg> wrote in message
    news:bur5od$lan sr$1@ID-161723.news.uni-berlin.de...[color=blue]
    > Hello,
    >
    > I have:
    >
    > std::map<std::s tring, std::string> m;
    > std::string s1;
    > char *p = "hello world", *p1 = "value";
    > (a) s1 = p;
    >
    > is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
    > string now?[/color]
    Yes
    [color=blue]
    >
    > (b) m[p] = p1;
    >
    > Is that okay? will that create a valid entry into the map?[/color]
    Yes[color=blue]
    > Or should I use: map[s1] = "value"; ?[/color]
    No[color=blue]
    > Btw, can someone tell me about std::string and its reference count issues?[/color]

    That's implementation dependent.
    Say p1 and p2 are string objects and both contain the string "Sharad".
    Then an implementation could use reference counting till no modifying operation
    occurs on either of the strings.
    Read Scott Meyers MEC++ which talks about such cases of reference counting.

    Best wishes,
    Sharad


    Comment

    • Kamil Burzynski

      #3
      Re: question about std::string/map

      On Fri, 23 Jan 2004 14:51:31 +0200, lallous wrote:[color=blue]
      > std::map<std::s tring, std::string> m;
      > std::string s1;
      > char *p = "hello world", *p1 = "value";
      > (a) s1 = p;
      >
      > is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
      > string now?[/color]

      s1 will do its own copy
      [color=blue]
      > (b) m[p] = p1;
      >
      > Is that okay? will that create a valid entry into the map?
      > Or should I use: map[s1] = "value"; ?[/color]

      In m[p]=p1, at first string will be created with copy of p, then m[] will
      get is as argument, second string will be created with copy of p1 and
      then assigned to m[p]

      This is equivalent:
      string sp( p );
      string sp1( p1 );
      m[ sp ] = sp1;
      [color=blue]
      > Btw, can someone tell me about std::string and its reference count issues?[/color]

      AFAIK std::string does not have to do reference counting (but it is
      allowed to). If it does share string between its instances, it is
      copy-on-write algorithm.

      --
      Best regards from
      Kamil Burzynski

      Comment

      • Kevin Goodsell

        #4
        Re: question about std::string/map

        lallous wrote:
        [color=blue]
        > Hello,
        >
        > I have:
        >
        > std::map<std::s tring, std::string> m;
        > std::string s1;
        > char *p = "hello world", *p1 = "value";[/color]

        Don't do this. It makes use of a dangerous and deprecated language
        feature - the implicit conversion of a string literal to a (non-const)
        char pointer. Always use 'const char *' or 'const char *const' for
        pointing to a string literal. This way the compiler will warn you if you
        attempt to modify the string literal. Without it, your code will
        silently invoke undefined behavior if you attempt to modify a string
        literal.
        [color=blue]
        > (a) s1 = p;
        >
        > is (a) okay if I later set "p = NULL" ? s1 would have its own copy of the
        > string now?
        >
        > (b) m[p] = p1;
        >
        > Is that okay? will that create a valid entry into the map?
        > Or should I use: map[s1] = "value"; ?
        >
        > Btw, can someone tell me about std::string and its reference count issues?[/color]

        I don't believe there are any such issues. Whether or not std::string
        uses reference counting is an implementation issue, and transparent to
        the programmer.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        Working...