Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - How much memory can a program allocate for itself - and how is it determined?

Tags:

c

memory

Is there a limit to the amount of memory that can be allocated from a program? By that I mean, is there any protection from a program, for example, that allocates memory in an infinite loop?

When would the call to malloc() return a NULL pointer?

like image 302
sherrellbc Avatar asked Dec 04 '22 09:12

sherrellbc


1 Answers

Yes, there is a limit. What that limit is depends on many factors, including (but not limited to):

  • The instruction set of the program (32-bit binaries have a smaller address space than 64-bit binaries, for example).
  • How much memory the system has free. ("Memory" here includes virtual memory.)
  • Any artificial restrictions set by the system administrator or a privileged process (see, for example, setrlimit() and the (obsolete) ulimit() function).

When memory cannot be allocated, malloc() will return NULL. If the system is completely out of memory, your process may be terminated forcefully.

like image 168
cdhowie Avatar answered Jan 23 '23 15:01

cdhowie