简体   繁体   中英

Why cannot I allocate more memory than the available in RAM?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM