I have an array int Array[17000][10][6][6]
我有一个数组int数组[17000][10][6][6]
But I got segmentation fault when I declare it.
但是我在声明时出现了分割错误。
So far I have been just using small arrays, I know that I have to use malloc, but I don't know how and I've been having a had time trying to understand the tutorials that I found.
到目前为止,我一直在使用小数组,我知道我必须使用malloc,但是我不知道如何使用,而且我一直在尝试理解我找到的教程。
Thanks for your help in advance
提前谢谢你的帮助
Tamalero
Tamalero
3 个解决方案
#1
2
You can do it like in Tamalero's answer, but as you might glean it can become a bit unwieldy.
你可以像Tamalero的答案那样做,但当你收集起来的时候,可能会变得有点笨拙。
The alternative is to do the indexing manually, i.e. allocate a compact array of just the data (the integers), no pointer arrays.
另一种方法是手动进行索引,即只分配数据(整数)的紧凑数组,不分配指针数组。
Then do the indexing by applying the required expression to compute the offset from the start of the array to the desired element:
然后通过应用所需的表达式计算从数组开始到所需元素的偏移量来进行索引:
const size_t ws = 17000, xs = 10, ys = 6, zs = 6;
int *array = malloc(ws * xs * ys * zs * sizeof *array);
array[w * (xs * yz * zs) + x * (ys * zs) + y * zs + z] = 4711;
You can wrap the indexing into a macro of course to make it easier to repeat.
您可以将索引封装到一个宏中,以使其更容易重复。
This will use less memory, is much easier to set up, and quite possibly faster since it does (way) fewer memory accesses per actual access, i.e. 1 instead of one per dimension due to the pointer-chasing.
这将使用更少的内存,更容易设置,而且可能更快,因为它每次实际访问的内存访问更少,即1而不是每个维度都有一个内存访问,这是由于指针跟踪。
#2
1
Even simpler than either of the existing two answers (only appropriate when the dimensions are static, of course):
甚至比现有的两个答案都要简单(当然,只有当维度是静态的时候才适用):
int (*array)[10][6][6] = malloc(17000 * sizeof(*array));
array[w][x][y][z] = 4711; // ... "natural" indexing ...
free(array);
Plus, the type of array
is almost the same as in the OP's original code.
另外,数组的类型与OP的原始代码几乎相同。
#3
0
int w=17000, x = 10, y = 6, z = 6;
int ****array;
int i, j, k, l, m;
array = malloc(sizeof(int ***) * w); /* size of an integer times the i length */
for(i = 0; i < w; ++i)
{ array[i] = malloc(sizeof(int **) * x);
for(j = 0; j < x; ++j)
{ array[i][j] = malloc(sizeof(int *) * y);
for(k = 0; k < y; ++k)
{ array[i][j][k] = malloc(sizeof(int) * z);
}
}
}
/* Check array */
m=0;
for (i = 0; i < w; i++) {
for (j = 0; j < x; j++) {
for (k = 0; k < y; k++) {
for (l = 0; l < z; l++) {
array[i][j][k][l] = m++;
}
}
}
}
m=0;
for (i = 0; i < w; i++) {
for (j = 0; j < x; j++) {
for (k = 0; k < y; k++) {
for (l = 0; l < z; l++) {
array[i][j][k][l] = m;
}
}
}
}
/* free memory used to making the array */
for (i = 0; i < w; i++) {
for (j = 0; j < x; j++) {
for (k = 0; k < y; k++) {
free(array[i][j][k]);
array[i][j][k] = NULL;
}
free(array[i][j]);
array[i][j] = NULL;
}
free(array[i]);
array[i] = NULL;
}
free(array);
array = NULL;
printf("Array prepare done\n");
#1
2
You can do it like in Tamalero's answer, but as you might glean it can become a bit unwieldy.
你可以像Tamalero的答案那样做,但当你收集起来的时候,可能会变得有点笨拙。
The alternative is to do the indexing manually, i.e. allocate a compact array of just the data (the integers), no pointer arrays.
另一种方法是手动进行索引,即只分配数据(整数)的紧凑数组,不分配指针数组。
Then do the indexing by applying the required expression to compute the offset from the start of the array to the desired element:
然后通过应用所需的表达式计算从数组开始到所需元素的偏移量来进行索引:
const size_t ws = 17000, xs = 10, ys = 6, zs = 6;
int *array = malloc(ws * xs * ys * zs * sizeof *array);
array[w * (xs * yz * zs) + x * (ys * zs) + y * zs + z] = 4711;
You can wrap the indexing into a macro of course to make it easier to repeat.
您可以将索引封装到一个宏中,以使其更容易重复。
This will use less memory, is much easier to set up, and quite possibly faster since it does (way) fewer memory accesses per actual access, i.e. 1 instead of one per dimension due to the pointer-chasing.
这将使用更少的内存,更容易设置,而且可能更快,因为它每次实际访问的内存访问更少,即1而不是每个维度都有一个内存访问,这是由于指针跟踪。
#2
1
Even simpler than either of the existing two answers (only appropriate when the dimensions are static, of course):
甚至比现有的两个答案都要简单(当然,只有当维度是静态的时候才适用):
int (*array)[10][6][6] = malloc(17000 * sizeof(*array));
array[w][x][y][z] = 4711; // ... "natural" indexing ...
free(array);
Plus, the type of array
is almost the same as in the OP's original code.
另外,数组的类型与OP的原始代码几乎相同。
#3
0
int w=17000, x = 10, y = 6, z = 6;
int ****array;
int i, j, k, l, m;
array = malloc(sizeof(int ***) * w); /* size of an integer times the i length */
for(i = 0; i < w; ++i)
{ array[i] = malloc(sizeof(int **) * x);
for(j = 0; j < x; ++j)
{ array[i][j] = malloc(sizeof(int *) * y);
for(k = 0; k < y; ++k)
{ array[i][j][k] = malloc(sizeof(int) * z);
}
}
}
/* Check array */
m=0;
for (i = 0; i < w; i++) {
for (j = 0; j < x; j++) {
for (k = 0; k < y; k++) {
for (l = 0; l < z; l++) {
array[i][j][k][l] = m++;
}
}
}
}
m=0;
for (i = 0; i < w; i++) {
for (j = 0; j < x; j++) {
for (k = 0; k < y; k++) {
for (l = 0; l < z; l++) {
array[i][j][k][l] = m;
}
}
}
}
/* free memory used to making the array */
for (i = 0; i < w; i++) {
for (j = 0; j < x; j++) {
for (k = 0; k < y; k++) {
free(array[i][j][k]);
array[i][j][k] = NULL;
}
free(array[i][j]);
array[i][j] = NULL;
}
free(array[i]);
array[i] = NULL;
}
free(array);
array = NULL;
printf("Array prepare done\n");