在visual studio C中分配大内存

时间:2013-11-21 17:28:35

标签: c visual-studio-2010

我在窗口中使用visual studio C来编写一个分配大内存的函数。如果我用小值测试它(小于3000x6000-3000是行数,6000是列数),它将是正确的。但是,当我使用大值(30000,60000)进行测试时,将显示错误消息“LargeMemory.exe中0x0f6bc9d0处的未处理异常:0xC0000005:访问冲突写入位置0x00000000”。 -0x0f6bc9d0是2D点的地址。如何解决这个问题。这是我的代码

    #include "stdafx.h"
    #include "stdio.h"
    #include "stdlib.h"
    #include <memory.h>
    int** memoryAllocation()
    {
          int nRow=30000;
          int nColumn=60000; 
          int **ppnGMatrix=NULL;
          ppnGMatrix =(int**)malloc(sizeof(int*)*nRow);
          if(ppnGMatrix!=NULL)
          {
            for (int nRowIndex = 0; nRowIndex < nRow; nRowIndex++)
            {
                ppnGMatrix[nRowIndex] = (int*)malloc(sizeof(int) * nColumn);
                memset(ppnGMatrix[nRowIndex], 0, sizeof(int) * nColumn);
            }
          }
          return ppnGMatrix;
    }
    int _tmain(int argc, _TCHAR* argv[])
    {

        int** GMatrixAllocate=NULL;
        GMatrixAllocate=memoryAllocation();
        return 0;
    }

1 个答案:

答案 0 :(得分:2)

您正在尝试分配1.8 * 4GB的内存。

首先:确保您正在编译为64位可执行文件,1.8 * 4GB将使用比32位可用内存空间更多的内存空间。即使你的地址空间中有足够的空间,它也可能不是连续的(所有在一个块中),这是malloc将要寻找的。

第二:重新设计您的代码,只要您需要这么大的连续块,这可能意味着您需要重新考虑您的方法。