Possible Duplicate:
Getting segmentation fault SIGSEGV in memcpy after mmap
I’m using mmap() in my cpp code to map a large size area (100,000,000 bytes ~ 100MB).
From man mmap I understand that I can only know whether it succeeded or not, I cannot know how much size it succeeded to map.
In my case, I could read that area iteratively with a buffer of 8192 bytes, but after I read ~24MB I get SIGSEGV – means that mmap didn’t map the entire area successfully?
I’m reading by a memcpy function to copy from the mapped area to my buffer on the heap. (I see the same behavior also when the buffer is on the stack).
How can I know if it mapped the entire area or not? And if it mapped the entire area then why do I get the SIGSEGV after around 24MB of bytes read?
Thanks!
So, your termination condition looks wrong: you decrement
read_lengthand loop until it is less thanbuffer_size.Also, you’re incrementing
addrbybuffer_sizeintegers (it is anint*), not bytes. So, you’re advancingaddr4 times too fast.BTW, in other code:
lseektakes and returns anoff_t, not asize_t.Edit: most of these bugs are pointed out in the other question already, so I’m not sure this one adds anything.