c - How to get the largest available chunk of memory that can be allocated? -
this question has answer here:
- how big can malloc in c? 6 answers
how can largest size (continious) in bytes application can allocate in 1 malloc call without returning out of memory? (biggest available chunk)
p.s.: general answer both if want allocate , if don't want allocate (so question not need reasked).
i presume asking (somewhat) modern desktop or server os.
the problem is: answer can invalid when result returned. reasons other processes, threads, memory fragmentation, etc. others stated in comments already, os might report more memory available - inlcuding swap. idea allocated memory might sparsely used process, accesses memory pages provided required, malloc() not reserve memory in advance.
it not idea allocate memory possible, can result in exessive swapping or starving/thrashing other processes. so, allocate memory need.
**conclusion: ** forget it! allocate memory require, not more, not less. if need dynamic memory, think dynamic structures lists , trees or use realloc() (but use thought).
sidenote:
do not feel tempted try allocated blocks in increasing size until null returned. can result in fragmentation, , might not able allocate previous largest block size (for similar reasons stated above).
for embedded oses, there might different answer.
Comments
Post a Comment