Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Malloc是C中的一个三维数组?_C_Arrays_Multidimensional Array - Fatal编程技术网

Malloc是C中的一个三维数组?

Malloc是C中的一个三维数组?,c,arrays,multidimensional-array,C,Arrays,Multidimensional Array,我正在将一些MATLAB代码翻译成C,我正在转换的脚本大量使用了具有10*100*300复杂条目的3D数组。阵列的大小也取决于传感器的输入,理想情况下,阵列应该动态分配。到目前为止,我已经尝试了两种方法,第一种是沿着 value = array[x + (y*xSize) + (z*ySize*xSize)] 这会伤害我的大脑。我还尝试了指针数组 int main () { int ***array = malloc(3*sizeof(int**)); int i, j; for

我正在将一些MATLAB代码翻译成C,我正在转换的脚本大量使用了具有10*100*300复杂条目的3D数组。阵列的大小也取决于传感器的输入,理想情况下,阵列应该动态分配。到目前为止,我已经尝试了两种方法,第一种是沿着

value = array[x + (y*xSize) + (z*ySize*xSize)]
这会伤害我的大脑。我还尝试了指针数组

int main () {
  int ***array = malloc(3*sizeof(int**));
  int i, j;

  for (i = 0; i < 3; i++) {
    *array[i] = malloc(3*sizeof(int*));
    for (j = 0; j < 3; j++) {
      array[i][j] = malloc(3*sizeof(int));
    }
  }

  array[1][2][1] = 10;

  return 0;
}
int main(){
int***数组=malloc(3*sizeof(int**));
int i,j;
对于(i=0;i<3;i++){
*数组[i]=malloc(3*sizeof(int*);
对于(j=0;j<3;j++){
数组[i][j]=malloc(3*sizeof(int));
}
}
数组[1][2][1]=10;
返回0;
}
当我试图分配数据时,会出现seg故障


在一个完美的世界中,我想使用第二种方法和数组表示法来实现更干净、更简单的编程。有没有更好的方法在C语言中动态分配一个三维数组?

我选择第一个选项(单个1D数组),因为它将为您提供一个单独的内存块,而不是潜在的数千个碎片内存块

但是,如果访问数组的正确元素会让你头疼,我会编写一个实用方法,将x、y、z位置转换为一维数组的偏移量

int offset(int x, int y, int z) { 
    return (z * xSize * ySize) + (y * xSize) + x; 
}

哦,我讨厌malloc数组分配吗^^

这是一个正确的版本,基本上只是一行错误的代码:

int main () {
  int ***array = (int***)malloc(3*sizeof(int**));
  int i, j;

  for (i = 0; i < 3; i++) {
    // Assign to array[i], not *array[i] (that would dereference an uninitialized pointer)
    array[i] = (int**)malloc(3*sizeof(int*));
    for (j = 0; j < 3; j++) {
      array[i][j] = (int*)malloc(3*sizeof(int));
    }
  }

  array[1][2][1] = 10;

  return 0;
}
int main(){
int***数组=(int***)malloc(3*sizeof(int**));
int i,j;
对于(i=0;i<3;i++){
//分配给数组[i],而不是*数组[i](这将取消对未初始化指针的引用)
数组[i]=(int**)malloc(3*sizeof(int*);
对于(j=0;j<3;j++){
数组[i][j]=(int*)malloc(3*sizeof(int));
}
}
数组[1][2][1]=10;
返回0;
}

在C89中无法实现您想要的功能,因为C中的数组类型只能用编译时已知的值来指定。所以为了避免疯狂的动态分配,你必须坚持一维的方式。您可以使用一个函数来简化此过程

int index(int x, int y, int z) {
  return x + (y*xSize) + (z*ySize*xSize);
}

int value = array[index(a, b, c)];
在C99中,即使维度是运行时值,也可以使用普通数组语法:

int (*array)[X][Y][Z] = (int(*)[X][Y][Z])malloc(sizeof *p); 
// fill...
int value = (*array)[a][b][c];
但是,它仅适用于本地非静态阵列

add#include“stdlib.h”并删除*from*数组[i],它将在Ubuntu上的GCC4.4.1中编译时运行

此外,如果您添加打印语句,您可以更快地发现错误

#include <stdio.h>
#include <stdlib.h>

int main () {
  int ***array = malloc(3*sizeof(int**));
  int i, j;

  printf("%s\n","OK");

  for (i = 0; i < 3; i++) {
    printf("i = %i \n",i);
    array[i] = malloc(3*sizeof(int*));
    for (j = 0; j < 3; j++) {
      printf("i,j = %i,%i \n",i,j);
      array[i][j] = malloc(3*sizeof(int));
    }
  }

  array[1][2][1] = 10;

  return 0;
}
#包括
#包括
int main(){
int***数组=malloc(3*sizeof(int**));
int i,j;
printf(“%s\n”、“确定”);
对于(i=0;i<3;i++){
printf(“i=%i\n”,i);
数组[i]=malloc(3*sizeof(int*);
对于(j=0;j<3;j++){
printf(“i,j=%i,%i\n”,i,j);
数组[i][j]=malloc(3*sizeof(int));
}
}
数组[1][2][1]=10;
返回0;
}

正如其他人所说,最好分配一个连续的内存块,然后自己计算索引。如果需要,可以编写一个函数来执行此操作。但是,由于您似乎对如何处理多个
malloc()
案例感兴趣,下面是一个示例:

首先,我定义了一个函数
free_data()
,它释放
int***
,其中
xlen
ylen
作为前两个维度大小。我们不需要
zlen
参数,就像
free()
不获取被释放指针的长度一样

void free_data(int ***data, size_t xlen, size_t ylen)
{
    size_t i, j;

    for (i=0; i < xlen; ++i) {
        if (data[i] != NULL) {
            for (j=0; j < ylen; ++j)
                free(data[i][j]);
            free(data[i]);
        }
    }
    free(data);
}
注意,我对
malloc
调用进行了相当多的简化:一般来说,不应该强制转换
malloc
的返回值,而应该将分配给它的对象指定为
sizeof
运算符的操作数,而不是它的类型。这使得
malloc
调用更易于编写,也更不容易出错。对于
malloc
,您需要包括
stdlib.h

以下是使用上述两个功能的测试程序:

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
    int ***data;
    size_t xlen = 10;
    size_t ylen = 100;
    size_t zlen = 300;
    size_t i, j, k;

    srand((unsigned int)time(NULL));
    if ((data = alloc_data(xlen, ylen, zlen)) == NULL)
        return EXIT_FAILURE;

    for (i=0; i < xlen; ++i)
        for (j=0; j < ylen; ++j)
            for (k=0; k < zlen; ++k)
                data[i][j][k] = rand();

    printf("%d\n", data[1][2][1]);
    free_data(data, xlen, ylen);
    return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
内部主(空)
{
int***数据;
尺寸xlen=10;
尺寸=100;
尺寸=300;
尺寸i,j,k;
srand((无符号整数)时间(NULL));
如果((数据=alloc_数据(xlen、ylen、zlen))==NULL)
返回退出失败;
对于(i=0;i

如果你觉得更容易使用,一定要使用这种方法。一般来说,这比使用连续的内存块要慢,但是如果您发现上述方案的速度还可以,并且如果它使您的生活更轻松,您可以继续使用它。即使您不使用它,也很高兴知道如何使这样的方案起作用。

这样,您只能分配1块内存,动态数组的行为与静态数组类似(即相同的内存连续性)。 您也可以像普通的1-D阵列一样使用单个空闲(阵列)来释放内存

double*** arr3dAlloc(const int ind1, const int ind2, const int ind3)
{
  int i;
  int j;
  double*** array = (double***) malloc( (ind1 * sizeof(double*)) + (ind1*ind2 * sizeof(double**)) + (ind1*ind2*ind3 * sizeof(double)) );
  for(i = 0; i < ind1; ++i) {
    array[i] = (double**)(array + ind1) + i * ind2;
    for(j = 0; j < ind2; ++j) {
      array[i][j] = (double*)(array + ind1 + ind1*ind2) + i*ind2*ind3 + j*ind3;
    }
  }
  return array;
}
double***arr3dAlloc(常量int ind1、常量int ind2、常量int ind3)
{
int i;
int j;
double***数组=(double***)malloc((ind1*sizeof(double*)+(ind1*ind2*sizeof(double**))+(ind1*ind2*ind3*sizeof(double));
对于(i=0;i
您确定需要使用
malloc
?C允许本机创建多维数组:

int a2[57][13][7];
或者您可以通过以下方式使用
malloc

int (*a)[13][7]; // imitates 3d array with unset 3rd dimension
                 // actually it is a pointer to 2d arrays

a = malloc(57 * sizeof *a);    // allocates 57 rows

a[35][7][3] = 12; // accessing element is conventional

free(a); // freeing memory
#包括
#包括
#定义MAXX 3
#定义maxy4
#定义MAXZ 5
main()
{
int***p,i,j;
p=(int***)malloc(MAXX*sizeof(int**));
对于(i=0;i;
}

int (*a)[13][7]; // imitates 3d array with unset 3rd dimension // actually it is a pointer to 2d arrays a = malloc(57 * sizeof *a); // allocates 57 rows a[35][7][3] = 12; // accessing element is conventional free(a); // freeing memory

#include<stdio.h>
#include<stdlib.h>

#define MAXX 3
#define MAXY 4
#define MAXZ 5

main()
{
int ***p,i,j;
p=(int ***) malloc(MAXX * sizeof(int **));

for(i=0;i < MAXX;i++)
{
p[i]=(int **)malloc(MAXY * sizeof(int *));
for(j=0;j < MAXY;j++)
p[i][j]=(int *)malloc(MAXZ * sizeof(int));
}

for(k=0;k < MAXZ;k++)
for(i=0;i < MAXX;i++)
for(j=0;j < MAXY;j++)
p[i][j][k]= < something >;

}
int main () 
{
    int ***array = malloc(3*sizeof(int**));
    int i, j;

    for (i = 0; i < 3; i++) {
       array[i] = malloc(3*sizeof(int*));
       for (j = 0; j < 3; j++) {
          array[i][j] = malloc(3*sizeof(int));
       }
    }

    array[1][2][1] = 10;

    return 0;
}
#include <stdio.h>

int main () {
  int ***array = (int ***) malloc(3*sizeof(int**));
  int i, j;

  for (i = 0; i < 3; i++) {
    array[i] = (int **)malloc(3*sizeof(int*));
    for (j = 0; j < 3; j++) {
      array[i][j] = (int *)malloc(3*sizeof(int));
    }
  }

  array[1][2][1] = 10;
  printf("%d\n", array[1][2][1]);
  return 0;
}
int row3d = 4;
int column3d = 4;
int height3d =4;
int val3d =10;

int ***arr3d = (int***)malloc (row3d*sizeof(int**));
for (int i =0 ; i<column3d;i++)
{
    arr3d[i] = (int**)malloc (column3d*sizeof(int*));
    for (int j = 0;j<height3d;j++)
    {
        arr3d[i][j] = (int*)malloc (height3d*sizeof(int));

        for (int z =0;z<height3d;z++,val3d++)
        {
            arr3d[i][j][z]   = val3d;
        }
    }

}
// De allocation.
for (int i=0;i<row3d;i++)
{
    for(int j=0;j<column3d;j++)
    {
        free(arr3d[i][j]);
    }
}
free(arr3d);
arr3d = 0;
int ***array3d = malloc(3 * sizeof(int **));
int **array2d = malloc(3 * 3 * sizeof(int *));
int *array1d = malloc(3 * 3 * 3 * sizeof(int));

for (size_t i = 0; i < 3; i++) 
{
  array3d[i] = array2d + i * 3;
  for (size_t j = 0; j < 3; j++)
    array3d[i][j] = array1d + i * 3 * 3 + j * 3;
}

array[1][2][1] = 10;
array1d[z * ySize * xSize + y * xSize + x]
array3d[x][y][x]
for (i = 0; i < 3; i++) {
    *array[i] = malloc(3*sizeof(int*));
//  ^ we dont want to deference array twice
    for (j = 0; j < 3; j++) {
        array[i][j] = malloc(3*sizeof(int));
    }
}
    for (i = 0; i < 3; i++) {
        array[i] = malloc(3*sizeof(int*));
        for (j = 0; j < 3; j++) {
            array[i][j] = malloc(3*sizeof(int));
        }
    }
   int ***a;
     a  = (int***)malloc(sizeof(int**));
    *a  = (int**)malloc(sizeof(int*));
   **a  = (int*)malloc(sizeof(int));