Maximum size of malloc()

本文介绍了一种方法,通过使用malloc()函数进行递增的虚拟内存搜索,来确定操作系统允许进程占用的最大堆大小。这种方法涉及到两次二分搜索过程,最终找到最大可分配的内存块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

How can I know the maximum size of the heap I can occupy by malloc(). I use MS Visual Studio 2010?


There are operating system-dependent ways of finding out how much virtual memory is available for your process, but I do not know how to do this on windows. You can, however, find it out by doing a hunt+halving search, caling malloc with ever larger arguments until it fails, and then homing in on the value it balks at. Something like

for(i=1; v=malloc(i); i<<=1) free(v);

By this point you know that i/2 bytes is ok, while i bytes is not ok. Now do a bisection search for the actual maximum:

for(a=(i>>1), b=i; a < b-1;)
{
    c=(a+b)>>1;
    if(v=malloc(c)) { a=c; free(v); }
    else b=c;
}

At this point, a is the largest amount you can successfully allocate.

From: http://stackoverflow.com/questions/13202492/maximum-size-of-malloc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值