user936509
user936509

Reputation: 223

C++ Memory Mapped File Implementation

I have no problems implementing the memory mapped file. The question is. Assuming this returns a valid memory view.

void* pBuf = MapViewOfFile(hMapFile,  
        FILE_MAP_WRITE,
        0,                   
        0,      
        0);

Do i have alternatives to using memcpy to give data to it? for instance can i tell my application to store data in it? I really want something like char* buffer = new char[1073741824] where the new places the data in the memory mapped file. this seems logical. or will i have to write a wrapper that writes to the memory view using memcpy? that would be disapointing.

Upvotes: 2

Views: 2488

Answers (2)

user707582
user707582

Reputation:

You can use the placement new operator or you can cast the address to a structure or class you want to use. The advantage of placement new is that the constructor of the class will be called. If you use from then on that pointer you don't need to copy the data but read and write directly to it.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612784

Probably the easiest approach is to use the boost memory mapped file classes which give the additional benefit of being portable.

Upvotes: 3

Related Questions