分配用C大arrayes有没有办法用这个大小分配数组:如何在Linux
unsigned long M[2000][900000] ;
这是我所得到的,当我运行的程序(编译过程中没有错误)。
Processus arrêté (Process stopped)
分配用C大arrayes有没有办法用这个大小分配数组:如何在Linux
unsigned long M[2000][900000] ;
这是我所得到的,当我运行的程序(编译过程中没有错误)。
Processus arrêté (Process stopped)
unsigned long (*pM)[2000][900000] = malloc(sizeof *pM);
做这项工作。
使用方法如下
#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);
也有这样一个'malloc()'检查结果是不是null在我看来是强制性的。 – jdarthenay
@jdarthenay:呃,够公平的,现在这是一个更完整的例子... – alk
它不起作用:malloc()失败:无法分配内存 –
而看壳结构,他认为他是编码矩阵:D – WedaPashi
请不要指出这个数组的大小是6.7GB。你的机器上有多少内存? –
该阵列至少约7GB。你真的需要这么大的阵列吗?如果你有的话,你的逻辑/算法可能会遇到更深层次的问题。 – kaylum