如何在linux中使用C分配大型阵列

如何在linux中使用C分配大型阵列,c,linux,memory,allocation,C,Linux,Memory,Allocation,是否有方法分配具有此大小的数组: unsigned long M[2000][900000] ; 这是我运行程序时得到的结果(编译期间没有错误) 他做这项工作 像这样使用它 #define ROWS_MAX (2000) #define COLUMNS_MAX (900000) ... unsigned long (*pM)[ROWS_MAX][COLUMNS_MAX] = malloc(sizeof *pM); /* 1st test whether the allocation s

是否有方法分配具有此大小的数组:

unsigned long M[2000][900000] ;
这是我运行程序时得到的结果(编译期间没有错误)

他做这项工作

像这样使用它

#define ROWS_MAX (2000)
#define COLUMNS_MAX (900000)

...

unsigned long (*pM)[ROWS_MAX][COLUMNS_MAX] = malloc(sizeof *pM);

/* 1st test whether the allocation succeeded! */
if (NULL == pM)
{
  perror("malloc() failed");
  exit(EXIT_FAILURE);
}

/* Then initialise the array. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  for (size_t column = 0; column < COLUMNS_MAX; ++column)
  {
    (*pM)[row][column] = 42;
  }
}

/* Do something ... */
...

/* Deallocate, free the memory. */
free(pM);
#定义行数_MAX(2000)
#定义列的最大值(900000)
...
无符号长(*pM)[ROWS_MAX][COLUMNS_MAX]=malloc(sizeof*pM);
/*第一次测试分配是否成功*/
if(NULL==pM)
{
perror(“malloc()失败”);
退出(退出失败);
}
/*然后初始化数组*/
对于(行大小=0;行<行最大值;++行)
{
用于(大小列=0;列
另一种使用多个块或内存的方法是使用分散/稀疏阵列:

unsigned long ** ppM = malloc(ROWS_MAX * sizeof *ppM);
if (NULL == ppM)
{
  perror("malloc() for row pointers failed");
  exit(EXIT_FAILURE);
}

for (size_t row = 0; row < ROWS_MAX; ++row)
{
  ppM[row] = malloc(COLUMNS_MAX * sizeof *ppM[row]);
  if (NULL == ppM[row])
  {
    perror("malloc() for a column failed");
    exit(EXIT_FAILURE);
    /* If not exiting the process here (but probably return from the function
       we are in), we need to perform a clean-up on what had been allocated 
       so far. See below code for free()ing it as a hint how to approach this. */
  }
}

/* Then initialise the array. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  for (size_t column = 0; column < COLUMNS_MAX; ++column)
  {
    ppM[row][column] = 42; /* Note the difference how to access the array. */
  }
}

/* Do something ... */
...

/* Deallocate, free the memory. */
/* Free columns. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  free(ppM[row]);
}

/* Free row pointers. */
free(ppM);
unsigned long**ppM=malloc(ROWS\u MAX*sizeof*ppM);
如果(空==ppM)
{
perror(“行指针的malloc()失败”);
退出(退出失败);
}
对于(行大小=0;行<行最大值;++行)
{
ppM[row]=malloc(列_MAX*sizeof*ppM[row]);
如果(NULL==ppM[行])
{
perror(“列的malloc()失败”);
退出(退出失败);
/*如果不在此处退出进程(但可能从函数返回
我们在),我们需要对分配的资源进行清理
到目前为止,请参阅下面的免费代码()并将其作为如何实现此目的的提示*/
}
}
/*然后初始化数组*/
对于(行大小=0;行<行最大值;++行)
{
用于(大小列=0;列
查看shell配置,他认为自己正在编写矩阵:请不要相信此阵列的大小为6.7GB。您的机器上有那么多RAM吗?该阵列至少约为7GB。你真的需要这么大的阵列吗?如果你这样做的话,你的逻辑/算法可能会有更深层次的问题。我需要一个正好如此大小的数组(thnx供你评论,但它没有帮助,是的,我知道malloc是什么),为什么不给这个数组一个静态存储时间?这应该行得通。还有这样的
malloc()
检查结果不为null在我看来是强制性的。它不起作用:malloc()失败:无法分配内存。。或者选择分散的阵列。更新我的答案。@AbderrahmaneMechri:所以你想增加操作系统的交换空间。或者看看
ulimit
告诉您当前配置的每个进程允许的最大内存。我想您是指“稀疏数组”,不是吗?
#define ROWS_MAX (2000)
#define COLUMNS_MAX (900000)

...

unsigned long (*pM)[ROWS_MAX][COLUMNS_MAX] = malloc(sizeof *pM);

/* 1st test whether the allocation succeeded! */
if (NULL == pM)
{
  perror("malloc() failed");
  exit(EXIT_FAILURE);
}

/* Then initialise the array. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  for (size_t column = 0; column < COLUMNS_MAX; ++column)
  {
    (*pM)[row][column] = 42;
  }
}

/* Do something ... */
...

/* Deallocate, free the memory. */
free(pM);
unsigned long ** ppM = malloc(ROWS_MAX * sizeof *ppM);
if (NULL == ppM)
{
  perror("malloc() for row pointers failed");
  exit(EXIT_FAILURE);
}

for (size_t row = 0; row < ROWS_MAX; ++row)
{
  ppM[row] = malloc(COLUMNS_MAX * sizeof *ppM[row]);
  if (NULL == ppM[row])
  {
    perror("malloc() for a column failed");
    exit(EXIT_FAILURE);
    /* If not exiting the process here (but probably return from the function
       we are in), we need to perform a clean-up on what had been allocated 
       so far. See below code for free()ing it as a hint how to approach this. */
  }
}

/* Then initialise the array. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  for (size_t column = 0; column < COLUMNS_MAX; ++column)
  {
    ppM[row][column] = 42; /* Note the difference how to access the array. */
  }
}

/* Do something ... */
...

/* Deallocate, free the memory. */
/* Free columns. */
for (size_t row = 0; row < ROWS_MAX; ++row)
{
  free(ppM[row]);
}

/* Free row pointers. */
free(ppM);