C 试图分配连续内存块并使用3个索引访问它,但我的方法失败

C 试图分配连续内存块并使用3个索引访问它,但我的方法失败,c,pointers,dynamic-memory-allocation,C,Pointers,Dynamic Memory Allocation,我试图编写一个过程,让我分配大小为n1*n2*n3的连续内存块,并使用3个索引访问它,就像使用数组一样 int array[n1][n2][n3]; 我已经成功地(据我所知)管理了两个索引(见下面的示例) #包括 int main(){ //尺寸 常数int n1=2; 常数int n2=2; 整数**数组; //指针 数组=(int**)malloc(n1*sizeof(int*); //大小为n1xn2的连续内存块 数组[0]=(int*)malloc(n1*n2*sizeof(int))

我试图编写一个过程,让我分配大小为n1*n2*n3的连续内存块,并使用3个索引访问它,就像使用数组一样

int array[n1][n2][n3];
我已经成功地(据我所知)管理了两个索引(见下面的示例)

#包括
int main(){
//尺寸
常数int n1=2;
常数int n2=2;
整数**数组;
//指针
数组=(int**)malloc(n1*sizeof(int*);
//大小为n1xn2的连续内存块
数组[0]=(int*)malloc(n1*n2*sizeof(int));
//指针算法

对于(int i=0;i为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;
}
#包括
#包括
int
主要(){
int n1=2;
int n2=3;
int n3=4;
int i,j;
int***阵列;
数组=malloc(n1*sizeof(int**));
对于(i=0;i
您可能失踪了

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;
}
#包括
int main(){
//尺寸
常数int n1=2;
常数int n2=2;
常数int n3=2;
int***阵列;
//指针
数组=(int***)malloc(n1*sizeof(int**));
数组[0]=(int**)malloc(n1*n2*sizeof(int*);
//大小为n1xn2xn3的连续内存块
数组[0][0]=(int*)malloc(n1*n2*n3*sizeof(int));
//指针算法

对于(int i=0;itry just`array=(int*)malloc(n1n2*n3*sizeof(int**));`在分配
array[i]
之前,您需要另一个循环来分配
array[i][j]
。作为C,您可以使用VLA寻址和单个分配来实现这一点(假设您的工具链确实支持它,如果使用MS工具集,那么您就是SOL)。因此,我不确定这是否是您的选项。请不要强制执行
malloc
的返回,这是不必要的。请参阅:我认为在本例中,内存不会是连续的,因为您正在mallocing n1*n2个独立的内存块memory@DJames你对不连续性的怀疑是有根据的。一旦你介绍了多个
malloc
连续性不在窗口内。这个答案不提供单个连续区域(你发布的任何代码也不提供)。@DJames True。但是你可以使用索引。@WhozCraig:嗯,这个
数组[0]=(int*)malloc(n1*n2*sizeof(int));
分配一个连续的内存区域,其中包含“2D”的所有
int
s-数组。@alk是该数组的一个数组,但有不止一个(因此是循环)。因此,其背景不能类似于
int-arr[a][b][c];
,但您已经知道了。不要使用
malloc
的返回,这是不必要的。请参阅:
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;
}