Attaching basic_string to preallocated buffer

A

Alvin777

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?
 
B

Bob Hairgrove

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?

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>.
 
J

John Harrison

Alvin777 said:
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?

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(const char*);
...
private:
const char* buffer;
};

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top