Segfault for a memcpy overlap during mmap
I'm experiencing a mmap problem that I can't seem to solve. Here is the setup: I allocate a huge multidimensional array into memory with malloc, I fill it with my values, and then I want to save it in a file. The array contains 3200000000 bytes. The machine is a 64-bit one and has enough memory to do so.
Linux 2.6.32-27-server #49-Ubuntu SMP Thu Dec 2 02:05:21 UTC 2010 x86_64 GNU/Linux
The file creation with mmap works just fine, I get the ptrFile pointer. When I memcpy my array into that ptrFile pointer, the program crashes with a segfault.
After a few debugging, I see that the two buffers overlap. The data array begins at address 0x7FEC47FFF010 and the pointer returned by mmap is 0x7FEA2543E000. I tried memmove to solve t开发者_高级运维his, but the result is the same. If I restrict the size copied to the file to 1000 bytes, it works just fine.
How come the two allocated pointer overlap?
Thanks a lot!
-- J
Those memory regions do not overlap (as long as your mmap
block is the size you say it is). 0x7FEC47FFF010
- 0x7FEA2543E000
= 9,172,684,816
, which is much larger than the size of 3,200,000,000
that you have mentioned.
mmap() cannot extend the file. Before copying the data, you first have to enlarge file to the proper size, using, for example, ftruncate. Though using mmap to just copy data to a file is overkill, just using write(2) is sufficient.
Do you really need a mmap
? If the only thing you want to make is to save your memory block, then a simple write
or fwrite
of the malloc'ed area will suffice. You only have to be sure that they handle 64 bit sizes.
精彩评论