如何在C ++中声明10000×100000大小的整数矩阵 [英] how to declare 10000 x 100000 sized integer matrix in C++

查看:1216
本文介绍了如何在C ++中声明10000×100000大小的整数矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个大小为10000x100000的矩阵。我的RAM是4GB。它工作到第25次迭代(调试),但在第25次迭代后,我得到一个错误坏分配,但只有25%的RAM被使用,这意味着问题与内存无关。那么我该怎么办?



编辑:

  int ** arr; 
arr = new int * [10000];
for(i = 0; i <10000; i ++)
arr [i] = new int [100000]

我的分配在上面。

解决方案

如果你正在编译x64,你不应该有任何问题。



如果你正在编译x86 ),您可以启用 / LARGEADDRESSAWARE 链接器标志,如果你使用Visual C ++,或类似的其他编译器。对于Visual C ++,该选项也可以在IDE中的Linker - > System - > Enable Large Addresses属性中找到。



这会在生成的EXE文件中设置一个标志告诉操作系统代码可以处理超过2 GB的地址。当在x64 Windows(你的case)上运行这样的可执行文件时,操作系统给它4 GB的地址空间来玩,而不是只有2 GB正常。



测试你的代码在我的系统,Windows 7 x64,8 GB,使用Visual C ++ Express 2013(x86,当然)用链接器标志编译,代码运行良好 - 分配近4 GB没有错误。



无论如何,第25次迭代太快了,无论它运行的是什么以及它是如何编译的(它大约是10 MB),它的失败,所以有其他的东西出了问题。 / p>

顺便说一下, HEAP 链接器选项在这种情况下没有帮助,因为它不会增加最大堆大小,它只是指定最初要预留多少地址空间以及以什么块来增加已提交RAM的数量。总之,它主要用于优化目的。


I need to create a matrix which size is 10000x100000. My RAM is 4GB. It works till the 25th iteration (debug), but after 25th iteration I get an error "bad allocation" however only 25% of RAM is used which means the problem is not related with memory. So what can I do?

EDIT:

int **arr;
arr=new int*[10000];
for(i=0;i<10000;i++)
    arr[i]=new int[100000];

My allocation is above.

解决方案

If you're compiling for x64, you shouldn't have any problems.

If you're compiling for x86 (most likely), you can enable the /LARGEADDRESSAWARE linker flag if you're using Visual C++, or something similar for other compilers. For Visual C++, the option can also be found in the Linker -> System -> Enable Large Addresses property in the IDE.

This sets a flag in the resulting EXE file telling the OS that the code can handle addresses over 2 GB. When running such an executable on x64 Windows (your case), the OS gives it 4 GB of address space to play with, as opposed to just 2 GB normally.

I tested your code on my system, Windows 7 x64, 8 GB, compiled with Visual C++ Express 2013 (x86, of course) with the linker flag, and the code ran fine - allocated almost 4 GB with no error.

Anyway, the 25th iteration is far too quick for it to fail, regardless of where it runs and how it's compiled (it's roughly 10 MB), so there's something else going wrong in there.

By the way, the HEAP linker option doesn't help in this case, as it doesn't increase the maximum heap size, it just specifies how much address space to reserve initially and in what chunks to increase the amount of committed RAM. In short, it's mostly for optimization purposes.

这篇关于如何在C ++中声明10000×100000大小的整数矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆