Reputation: 53
I want to determine what is the maximum limit of memory I can allocate in my computer. This is the code that I have written for this task:
#include <stdio.h>
#include <stdlib.h>
int main() {
int j;
int *primes;
int i ;
int limit = 2147483647;
primes = malloc(sizeof(int) * limit);
for (i = 0; i < limit; i++)
{
primes[i] = 1;
}
return 0;
}
How can I determine how much memory can be allocated without hit and trial? I have allocated the maximum int size in this example. But the program crashes. How much memory is really being allocated in this example?
Upvotes: 5
Views: 8313
Reputation: 21627
The maximum amount of memory you can allocate is controlled by several factors and can change over time.
These include: 1. Hardware limits 2. OS Limits 3. System Parameters 4. Process Quotas 5. Page file space
In addition, malloc is a very poor way to allocate large blocks of memory.
Your program crashes because malloc is returning null and you are using that return value without checking.
Upvotes: 0
Reputation: 1045
There are so many iteration perform by loop,you design that is the main causes of your program crash or loop dead infinity.
Answer to what you expect to know is very complex because of some key notes---->>
1. It depends on the platform that the program is working on like windows, linux or mac. I THINK that the amount of memory is not limited by anything, but physical memory.
Fact-> Although physical memory is Might be extended by Virtual memory, Not all platforms has feature of 'virtual memory'.
C has no concept of virtual memory.
Malloc allocates contiguous memory (meaning side by side or together in ram).
So, it depends on the way platform handles the request. It depends upon the C implementation.
2. The largest number (in bytes) representable by standard type 'size_t' (declared by ). This value can and does vary among implementations. Note that this value isn't necessarily as large as the host(i.e. end user) platform's available memory.
QUES. Is there any limitation on that? Where am I supposed to get this kind of information?
Ans. Malloc's argument is a size_t and the range of that type is [0,SIZE_MAX], so the maximum you can request is SIZE_MAX, which value varies from implementation to implementation and is defined in .
Note:- Whether a request for SIZE_MAX bytes will succeed depends on factors outside of the scope of this group.
Upvotes: -1
Reputation: 134336
You code is wrong for so many reasons like
You're assuming that malloc()
offers partial allocation (up to the available memory) which is again, wrong.
Quoting the C11
standard, chapter §7.22.3.4, (emphasis mine)
The
malloc
function returns either a null pointer or a pointer to the allocated space.
So, it's either complete success (with the exact amount of requested memory allocated) or complete failure (return of null pointer), there's no trade-off like partial success with available memory or whatever which you might have assumed.
You're not checking for malloc()
success, resulting probable null-pointer dereference.
I believe, you're in need of getrlimit()
and family to achieve your purpose. Particular point of interest would be RLIMIT_DATA
as the resource
value.
That said,
1. "I have allocated the maximum int size in this example"
This does not seem to be connected with the limits for malloc()
and family, anyways.
2. "But the program crashes"
That is most probably the result of undefined behavior. In your code, you directly dereference the returned pointer, without success check for malloc()
. It is likely that malloc()
has failed and returned a NULL
, dereferencing that causes the UB.
Upvotes: 4
Reputation: 5856
malloc()
is allowed to fail, in which case it returns a NULL-pointer without allocating any memory. It is always an all-or-nothing allocation. It either succeeds and allocates a full chunk of memory of the requested size, or it fails, returning a NULL-pointer without allocating a single byte.
As for knowing how much memory is available - that really depends on the environment: what OS are you running this in, is that a 16-, 32-, 64-bit memory architecture?
For instance, if you are running on Windows 10 you can use the GlobalMemoryStatusEx()
facility (refer to MSDN:GlobalMemoryStatusEx() for details).
Linux, OTOH, provides a neat sysconf()
way of retreiving similar information. Refer to this page for more details.
Even if your OS is 64-bit that wouldn't necessarily mean that your application can access more than a certain limit. For instance Windows 7 64-bit will only let you address up to 8GB of memory in your application code even though the full virtual memory space is 16TB.
Upvotes: 8