Reputation: 393
I was trying to execute a malloc sentence like the following one:
TYPODATO *prof=(TYPODATO *)malloc((size_t)H*V*B2*sizeof(TYPODATO));
with being H*V*B2*sizeof(TYPODATO)
equal to 13037160840 B = 13.04 GB. If I execute the command free -m
in the console to obtain the available memory in MB, I get 13486 MB = 13.486 GB.
However, the previous malloc statement returns a NULL pointer, indicating that such an amount of memory cannot be allocated. The requested amount of memory is not as much as the available in the system, but even if it was I should be able to successfully allocate it, cause the system can use the virtual memory. Why is not the system using the virtual memory in this case?
Upvotes: 0
Views: 117
Reputation: 227
malloc()
does not work this way. malloc() allocates a single contiguous block of memory.
You are trying to allocate more memory than contiguously available, hence the error.
Upvotes: 4