I'm mapping a file view to share an array of 200 std::strings between two processes but I've been getting an access violation error when I write a string longer than 15 characters into the array. Does anyone know how I might increase the size of each array element so it doesn't overflow and cause problems?

struct _sSharedArray {
	std::string strData[ 200 ];
} *sSharedArray = NULL;

HANDLE sSeedFileHandle;

HANDLE sFileMappingHandle;


sFileMappingHandle = CreateFileMapping( sSeedFileHandle  , 0 , PAGE_READWRITE , 0 , 10000 , "sSharedMap"  );

sFileMappingHandle = OpenFileMapping( FILE_MAP_WRITE , 1 , "sSharedMap" );

sSharedArray = ( struct _sSharedArray* ) MapViewOfFile( sFileMappingHandle , FILE_MAP_WRITE , 0 , 0 , 0 );

MapViewOfFile returns the starting address of the mapped view so I set that to the starting address of the array.

Now if I write a string into the array...

sSharedArray->strData[x] = "This string will cause an access violation!";

sSharedArray->strData[x] = "This will not.";

Note : Error checking and comments removed.

8 Years
Discussion Span
Last Post by Liinker

I have not actually worked with file mapping, but I suspect the problem is that std::string attempts to allocate memory which causes the file mapping to crash. Try replacing std::string strData[ 200 ]; with character arrays char strData[200][255] and see if that fixes the problem.


After about an hour of not receiving any responses yesterday, I decided to do exactly that. Although, slightly different. I changed the array definition to...

struct _FixedLengthIndex {
	char cChar[30];
} *pFixedLengthIndex = NULL;

struct _sSharedArray {
	_FixedLengthIndex sIndex[ 200 ];
} *pSharedArray = NULL;

Basically the same thing. I still get an access violation but now i can write a 24 character string. Any more causes the error. I also increased the string length to 50 chars... but it still limited me to 24 before causing the error. I'm going to try your multi-dimensional array and report back. Thanks for the suggestion.


I tried your multi-dimension suggestion and I still have the 24 character limit. I'm scratching my head here... I guess I'll keep trying different things.


Well I solved the access violation error by iterating through the array when its first initialized and writing an 'x' to each char. I'm guessing this allocated the correct amount of memory while the dll was being loaded rather than after the additional modules were loaded.

But now when I increase the total number of characters beyond 5000... I get a OutOfMemory exception. Trying to work through that now...


Have you tried different file attributes and access levels, like just all read, no hidden stuff, etc.

Unfortunately both processes need to read and write to the mapped file, although at different times, and the hidden attribute was just recently added for cosmetic reasons. I get the same error regardless.

I'm thinking that because the mapped file is inside the memory space for each process, I need to somehow increase the memory space size to accommodate for the file.

Thats really my only lead right now.


Problem solved. I must have been exceeding the allocated memory space for my application. I actually was mapping (4) 10k files into the space but was only using one of them. After removing the unused 3 files, I was able to increase the index lengths to 100 characters.

This question has already been answered. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.