尝试分配一个连续的内存块并使用 3 个索引访问它,但我的方法出现段错误
Attempting to allocate a contiguous memory block and access it using 3 indices, but my method segfaults
我正在尝试编写一个程序,让我分配一个大小为 n1*n2*n3 的连续内存块,并使用 3 个索引访问它,就像使用数组一样
int array[n1][n2][n3];
我已经(据我所知)成功地用两个索引管理了这个(见下面我的例子)
#include <stdlib.h>
int main() {
// Dimensions
const int n1 = 2;
const int n2 = 2;
int **array;
// Pointers
array = (int **)malloc(n1*sizeof(int *));
// Contiguous chunk of memory of size n1xn2
array[0] = (int *)malloc(n1*n2*sizeof(int));
// Pointer arithmetic
for(int i=0;i<n1;i++) {
array[i] = array[0] + i*n2;
}
array[0][0] = 1;
return EXIT_SUCCESS;
}
但是当我尝试使用三个索引的类似构造时,我的程序抛出一个段错误:
#include <stdlib.h>
int main() {
// Dimensions
const int n1 = 2;
const int n2 = 2;
const int n3 = 2;
int ***array;
// Pointers
array = (int ***)malloc(n1*sizeof(int **));
array[0] = (int **)malloc(n1*n2*sizeof(int *));
// Contiguous chunk of memory of size n1xn2xn3
array[0][0] = (int *)malloc(n1*n2*n3*sizeof(int));
// Pointer arithmetic
for(int i=0;i<n1;i++) {
for(int j=0;j<n2;j++) {
array[i][j] = array[0][0] + i*n2*n3 + j*n2;
}
}
array[0][0][0] = 1;
return EXIT_SUCCESS;
}
我知道还有其他方法可以管理连续的内存块。我对为什么我的逻辑在上述情况下失败特别感兴趣。
正在为 3D 数组分配内存。
也许我做错了什么,但看起来还可以。
#include <stdlib.h>
#include <stdio.h>
int
main () {
int n1 = 2;
int n2 = 3;
int n3 = 4;
int i, j;
int ***array;
array = malloc(n1 * sizeof(int**));
for (i = 0; i < n1; i++)
array[i] = malloc(n2 * sizeof(int*));
for (i = 0; i < n1; i++)
for (j = 0; j < n2; j++)
array[i][j] = malloc(n3 * sizeof(int));
array[1][2][3] = 15000;
printf("%d\n", array[1][2][3]);
return 0;
}
你可能错过了
array[i] = array[0] + i*n2;
这是您的代码
#include <stdlib.h>
int main() {
// Dimensions
const int n1 = 2;
const int n2 = 2;
const int n3 = 2;
int ***array;
// Pointers
array = (int ***)malloc(n1*sizeof(int **));
array[0] = (int **)malloc(n1*n2*sizeof(int *));
// Contiguous chunk of memory of size n1xn2xn3
array[0][0] = (int *)malloc(n1*n2*n3*sizeof(int));
// Pointer arithmetic
for(int i=0;i<n1;i++) {
array[i] = array[0] + i*n2;
for(int j=0;j<n2;j++) {
array[i][j] = array[0][0] + i*n2*n3 + j*n2;
}
}
array[0][0][0] = 1;
return EXIT_SUCCESS;
}
我正在尝试编写一个程序,让我分配一个大小为 n1*n2*n3 的连续内存块,并使用 3 个索引访问它,就像使用数组一样
int array[n1][n2][n3];
我已经(据我所知)成功地用两个索引管理了这个(见下面我的例子)
#include <stdlib.h>
int main() {
// Dimensions
const int n1 = 2;
const int n2 = 2;
int **array;
// Pointers
array = (int **)malloc(n1*sizeof(int *));
// Contiguous chunk of memory of size n1xn2
array[0] = (int *)malloc(n1*n2*sizeof(int));
// Pointer arithmetic
for(int i=0;i<n1;i++) {
array[i] = array[0] + i*n2;
}
array[0][0] = 1;
return EXIT_SUCCESS;
}
但是当我尝试使用三个索引的类似构造时,我的程序抛出一个段错误:
#include <stdlib.h>
int main() {
// Dimensions
const int n1 = 2;
const int n2 = 2;
const int n3 = 2;
int ***array;
// Pointers
array = (int ***)malloc(n1*sizeof(int **));
array[0] = (int **)malloc(n1*n2*sizeof(int *));
// Contiguous chunk of memory of size n1xn2xn3
array[0][0] = (int *)malloc(n1*n2*n3*sizeof(int));
// Pointer arithmetic
for(int i=0;i<n1;i++) {
for(int j=0;j<n2;j++) {
array[i][j] = array[0][0] + i*n2*n3 + j*n2;
}
}
array[0][0][0] = 1;
return EXIT_SUCCESS;
}
我知道还有其他方法可以管理连续的内存块。我对为什么我的逻辑在上述情况下失败特别感兴趣。
正在为 3D 数组分配内存。 也许我做错了什么,但看起来还可以。
#include <stdlib.h>
#include <stdio.h>
int
main () {
int n1 = 2;
int n2 = 3;
int n3 = 4;
int i, j;
int ***array;
array = malloc(n1 * sizeof(int**));
for (i = 0; i < n1; i++)
array[i] = malloc(n2 * sizeof(int*));
for (i = 0; i < n1; i++)
for (j = 0; j < n2; j++)
array[i][j] = malloc(n3 * sizeof(int));
array[1][2][3] = 15000;
printf("%d\n", array[1][2][3]);
return 0;
}
你可能错过了
array[i] = array[0] + i*n2;
这是您的代码
#include <stdlib.h>
int main() {
// Dimensions
const int n1 = 2;
const int n2 = 2;
const int n3 = 2;
int ***array;
// Pointers
array = (int ***)malloc(n1*sizeof(int **));
array[0] = (int **)malloc(n1*n2*sizeof(int *));
// Contiguous chunk of memory of size n1xn2xn3
array[0][0] = (int *)malloc(n1*n2*n3*sizeof(int));
// Pointer arithmetic
for(int i=0;i<n1;i++) {
array[i] = array[0] + i*n2;
for(int j=0;j<n2;j++) {
array[i][j] = array[0][0] + i*n2*n3 + j*n2;
}
}
array[0][0][0] = 1;
return EXIT_SUCCESS;
}