此文版权属于作者所有,任何人、媒体或者网站转载、借用都必须征得作者本人同意!
32
位的程序寻址空间是
4G
,因此能用的内存应该有
4G
,除掉一些系统等使用的乱七八糟的东西,
3G
内存应该没有问题吧,这些只是猜测,写个程序测一下,结果如下:
测试结论:
1.
栈内存最大可用
768k
左右;
2.
堆内存最大可用
1.586G
左右。
结果令人沮丧,才
1.6 G!!!
测试环境:
Windows7 2G
、
IBMSL300
、
VC9
附录测试程序等
测试程序只考虑
windows
,首先,说明一下相关知识:
内存可以来自两个地方,在栈空间上分配的内存(栈内存)和在堆空间分配的内存(堆内存)。
栈内存
,通过
alloca
来申请。
堆内存
,通过
malloc
、
new
、
VirtualAlloc
来申请。
测试程序如下:
/* $Id$ */
/**
* \file memory_test.cpp
*
* \brief
大内存申请测试
*
* \version $Rev$
* \author
![](http://hi.csdn.net/attachment/201110/19/0_13189938139CCL.gif)
* \date 2010
年04月06日:09:24
*
* \note
修改历史:<br>
* <table>
* <tr><th>
日期</th><th>修改人</th><th>内容</th></tr>
* <tr><td>2010-4-6</td><td>
</td><td>创建初稿</td>
![](http://hi.csdn.net/attachment/201110/19/0_13189938139CCL.gif)
* </tr>
* </table>
*/
#include
<stdio.h>
#include
<malloc.h>
#include
<windows.h>
#include
<excpt.h>
// alloca: Allocates memory on the stack
static
bool alloca_test(size_t s)
{
__try{
alloca(s); printf("
√
1 alloca(%u)
通过\n"
, s);
}__except( GetExceptionCode() == STATUS_STACK_OVERFLOW ){
printf("
×
1 alloca(%u)
失败,内存溢出\n"
, s);
_resetstkoflw();
return false;
}
__try{
alloca(s); printf("
√
2 alloca(%u)
通过\n"
, s);
}__except( GetExceptionCode() == STATUS_STACK_OVERFLOW ){
printf("
×
2 alloca(%u)
失败,内存溢出\n"
, s);
_resetstkoflw();
return false;
}
return true;
}
// malloc: Allocates memory blocks.
static
bool malloc_test(size_t s)
{
void* buf = malloc(s);
if (buf)
printf("
√
malloc(%u)
通过\n"
, s);
else
printf("
×
malloc(%u)
失败\n"
, s);
free(buf);
return buf != 0;
}
// malloc: Allocates memory blocks.
static
bool malloc_test2(size_t s)
{
void* buf1 = malloc(s);
void* buf2 = malloc(s);
if (buf1)
printf("
√
buf1 malloc(%u)
通过\n"
, s);
else
printf("
×
buf1 malloc(%u)
失败\n"
, s);
if (buf2)
printf("
√
buf2 malloc(%u)