I have mapped a file onto memory using mmap() and I need to write beyond end of file. I cannot find any conclusive documentation evidence of what happens in this case. This document says access beyond EOF is undefined. Is there any correct way to do this?
Share
Unmap the file, extend it by any conventional means (fseek to end + fwrite) and mmap it again it. This should be quite fast; any parts of the file that are paged in would stay paged in.
You can also extend the file while it is mapped and then mremap it. This should work fine, because you are not changing any of the mapped pages. The effect of this may be OS-dependent.
Linux mmap manpage: