Create Named Shared Memory files to transfer huge data

819 views Asked by At

Using MSDN's example on Creating Named Shared Memory, I have some questions (note: variable names inline are referred from the example process 1 in hyperlink):

  • Would it be possible to copy high length messages (ie. szMsg.length() > 4000) to buffer (pBuf) in process 1?

  • What is the max size of the buffer (BUF_SIZE) allowed for creating file mapping object?

  • If yes, what is the relation between buffer size used for creating mapping object vs length of the message.(ie.. In the example, BUF_SIZE vs szMsg)

I am asking these questions because when I try to copy messages of length > 4000, it fails with access violation errors, in spite of increasing buf_size from 512 to 1024 or even higher.

I am new to coding. Kindly explain to me how to create a file mapping object which could accommodate high length messages.

1

There are 1 answers

9
Martin Bonner supports Monica On

If you write to more than BUF_SIZE bytes, Windows does not guarantee what happens. In practise though, the file-mapping will be rounded up to a memory page (which happens to be 4096). Thus if you specify a "small" value for BUF_SIZE, you will get away with messages upto and including 4096 bytes long - but if you write 4097 bytes, you will will probably(*) get an access violation.

*: If you are unlucky, the next page will already be mapped into the process, and you will overwrite some other critical information.

Solution: You must set BUF_SIZE to be at least as large as the largest message you want to send.

What is the relation between buffer size used for creating mapping object vs length of the message.(ie.. In the example, BUF_SIZE vs szMsg)

A: BUFSIZE must be greater than or equal to szMsg.