MetallicPriest MetallicPriest - 1 year ago 79
C Question

Getting cannot allocate memory error

I am getting this error in my program...

mprotect: Cannot allocate memory

ulimit -a gives the output:

core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 20
file size (blocks, -f) unlimited
pending signals (-i) 16382
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) unlimited
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

The amount of memory I'm trying to protect is 60 MB. Can someone tell me what is the problem and how it can be solved?

Answer Source
Return Value

On success, mprotect() returns zero.
On error, -1 is returned, and errno is set appropriately.


The memory cannot be given the specified access. This can happen, for example, if
you mmap(2) a file to which you have read-only access, then ask mprotect() to
mark it PROT_WRITE.

The memory cannot be accessed.

addr is not a valid pointer, or not a multiple of PAGESIZE.

Internal kernel structures could not be allocated.
Or: addresses in the range [addr, addr+len] are invalid for the address space of
the process, or specify one or more pages that are not mapped.

Given the error message, you probably got an ENOMEM error, and looking at the error code, this does not necessarily mean that memory could not be allocated. You might have an invalid address range, or (most likely) you have pages that aren't mapped.

Don't try to protect such a big hunk of memory in one swell foop. Given how virtual memory works, the odds are just too high that some page in that huge chunk will not be mapped. You need to ensure that the page (pages) in question are mapped before calling mprotect.

When you are using system functions it is always a good idea to read the man page on that function. Then re-read it. The man pages can be a bit terse at times.