|
发表于 2014-1-24 22:52:33
|
显示全部楼层
本帖最后由 夏季 于 2014-1-25 09:53 编辑
C标准没有查过,不作评论。
根据malloc/new的情况看,堆中申请空间的上限是根据操作系统和编译环境(x86、x64)和剩余可用内存来定的。
注:x86可以申请的不能超过4G,实际上更小;x64的上限可以理解为操作系统剩余可用内存大小。
那么局部数组,猜测应该就是根据可分配栈的内存空间来定。另,栈的大小也是可以改的,不过一般不推荐。
// 第一份代码
- const unsigned int MAX_LENGTH = 30000;
- double *pData = NULL;
- //pData = (double *) malloc(sizeof(double)*MAX_LENGTH);
- pData = new double [Max_Length];
- if( NULL == pData ) // 检验是否申请到空间
- {
- printf("Need more memory\n");
- }
- if( NULL != pData )
- {
- //free(pData);
- delete [] pData;
- pData = NULL;
- }
复制代码
// 第二份代码,运行这个可以看到x86和x64在内存申请方面的区别。运行的时候可以看资源管理器的内存曲线变化
- #include <iostream>
- #include <map>
- #include <vector>
- #include <string>
- using namespace std;
- int main()
- {
- #if 0
-
- map<int, string>mapT;
- cout<<mapT.max_size()<<endl;
- for(size_t i = 0; i != mapT.max_size(); ++i)
- {
- try
- {
- mapT.insert(make_pair(i, string(1000, 's')));
- }
- catch(std::bad_alloc)
- {
- cout<<"\nNo enough menory to allow!\n"<<endl;
- cout<<mapT.size()<<" "<<mapT.max_size()<<endl;
- mapT.clear();
- //return 1;
- }
- if( i % 10000 == 0)
- cout<<i<<" "<<mapT.size()<<endl;
- }
-
- #else
- vector<string> stringSet;
- cout<<stringSet.max_size();
- for(size_t i = 0; i != stringSet.max_size(); ++i)
- {
- try
- {
- stringSet.push_back(string(1000, 's'));
- }
- catch(std::bad_alloc)
- {
- cout<<"\nNo enough menory to allow!\n"<<endl;
- cout<<stringSet.size()<<" "<<stringSet.max_size()<<endl;
- stringSet.clear();
- //return 1;
- }
- if( i % 10000 == 0)
- cout<<i<<" "<<stringSet.size()<<endl;
- }
- #endif
-
- return 0;
- }
复制代码
|
|