attaching & detaching buffer to standard string ?

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

    attaching & detaching buffer to standard string ?

    Let's say I have a
    string s;
    and I know exactly what allocator string s is using.

    Then I have separately allocated buffer
    char *buf = allocator<char> ::allocate(bufl en);
    which is initialized with same allocator as compatible with string s.

    1) How can I make 's' point to 'buf' (to 'attach') ?
    (to avoid copying that "string s(buf, len)" would do ?)

    2) How do I 'detach' buffer pointer from the string, that
    is obtain s.data() + simultaneously reset
    internal data ptr of s to NULL (and its length, too) ?

    Weird tricks (that work) are welcome ...

    Alex Perlov
    --
    Spellchecker: Misspelled word: "allocator"
    Spellchecker: Replace with: alligator

  • Attila Feher

    #2
    Re: attaching &amp; detaching buffer to standard string ?

    Alex Perlov wrote:[color=blue]
    > Let's say I have a
    > string s;
    > and I know exactly what allocator string s is using.
    >
    > Then I have separately allocated buffer
    > char *buf = allocator<char> ::allocate(bufl en);
    > which is initialized with same allocator as compatible with string s.
    >
    > 1) How can I make 's' point to 'buf' (to 'attach') ?
    > (to avoid copying that "string s(buf, len)" would do ?)[/color]

    You cannot - in standard C++, at least AFAIK.
    [color=blue]
    > 2) How do I 'detach' buffer pointer from the string, that
    > is obtain s.data() + simultaneously reset
    > internal data ptr of s to NULL (and its length, too) ?
    >
    > Weird tricks (that work) are welcome ...[/color]

    There are no portable weird tricks.

    --
    Attila aka WW


    Comment

    • Ron Natalie

      #3
      Re: attaching &amp; detaching buffer to standard string ?


      "Alex Perlov" <alex_p24@yahoo .com> wrote in message news:3f703368@n ews.bezeqint.ne t...
      [color=blue]
      > Then I have separately allocated buffer
      > char *buf = allocator<char> ::allocate(bufl en);
      > which is initialized with same allocator as compatible with string s.
      >[/color]
      You can't make any such assumptions. The way string manages it's
      internal storage is not defined by the standard. There's no requirement
      for it to be contiguous even.

      What do you really want to accomplish?


      Comment

      Working...