Attaching basic_string to preallocated buffer

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

    Attaching basic_string to preallocated buffer

    I'd like to have data file with many strings and load them at once
    using MapViewOfFile[Ex] and unload with UnmapViewOfFile . So I wouldn't
    have to allocate memory for each string on heap.

    How can I use basic_string<> attaching them to pointer to string
    within mapped space so it wouldn't copy that string to internal
    buffer?

    --
    Alvin777
  • John Harrison

    #2
    Re: Attaching basic_string to preallocated buffer


    "Alvin777" <alvin777@mail. ru> wrote in message
    news:1c9319e1.0 402210051.175e5 827@posting.goo gle.com...[color=blue]
    > I'd like to have data file with many strings and load them at once
    > using MapViewOfFile[Ex] and unload with UnmapViewOfFile . So I wouldn't
    > have to allocate memory for each string on heap.
    >
    > How can I use basic_string<> attaching them to pointer to string
    > within mapped space so it wouldn't copy that string to internal
    > buffer?
    >[/color]

    You cannot. Obviously there would be difficulties if, for instance,
    std::string wanted to reallocate its buffer. It would be awkward to keep
    track of whether it had a reallocated buffer, or one that was supplied by
    the user.

    I suggest you write your own string class that does what you want
    (presumably one that never reallocates its buffer).

    class StaticString
    {
    public:
    StaticString(co nst char*);
    ...
    private:
    const char* buffer;
    };

    john


    Comment

    • Bob Hairgrove

      #3
      Re: Attaching basic_string to preallocated buffer

      On 21 Feb 2004 00:51:58 -0800, alvin777@mail.r u (Alvin777) wrote:
      [color=blue]
      >I'd like to have data file with many strings and load them at once
      >using MapViewOfFile[Ex] and unload with UnmapViewOfFile . So I wouldn't
      >have to allocate memory for each string on heap.
      >
      >How can I use basic_string<> attaching them to pointer to string
      >within mapped space so it wouldn't copy that string to internal
      >buffer?[/color]

      You might find it easier to use std::iostream etc.

      I don't think you can do this with basic_string. But I would be gladly
      poven wrong <g>.


      --
      Bob Hairgrove
      NoSpamPlease@Ho me.com

      Comment

      Working...