简体   繁体   English

为三指针分配内存:C

[英]Allocating memory for triple pointer: C

I am trying to allocate memory for a triple pointer. 我正在尝试为三指针分配内存。 I have the following: 我有以下内容:

int i, j;
int n = 4;

int ***X = (int ***) malloc(sizeof(int) * n);
for(i = 0; i < n; i++){
      printf("h\n");
      X[i] = (int **) malloc(sizeof(int) * n);
      for(j = 0; j < n; j++){
            printf("j\n");
            X[i][j] = (int *) malloc(sizeof(int) * n);
      }
}

X[0][0][0] = 14;
X[1][2][2] = 15;

When I run this on Linux, I get *** glibc detected *** triplePointer: double free or corruption (out): 0x0000000000ea3050 *** error which I have completely no idea what it is implying. 当我在Linux上运行它时,我得到*** glibc detected *** triplePointer: double free or corruption (out): 0x0000000000ea3050 ***错误,我完全不知道这是什么意思。 But when I run it on Windows with the -Wall flag, I get no errors. 但是当我在Windows上使用-Wall标志运行它时,我没有错误。 Can someone perhaps help me to find where my error is at. 有人可以帮我找到我的错误所在。

Also, I am currently hard coding by having the statement X[0][0][0] = 14; 此外,我目前通过声明X[0][0][0] = 14;硬编码X[0][0][0] = 14; . Is there a way that I can populate all the slots of this triple pointer by some random values? 有没有办法可以通过一些随机值填充这个三重指针的所有插槽?

Try the following code- 请尝试以下代码 -

int ***X = (int ***) malloc(sizeof(int**) * n); //FIX 1
for(i = 0; i < n; i++){
  printf("h\n");
  X[i] = (int **) malloc(sizeof(int*) * n);  // FIX 2
  for(j = 0; j < n; j++){
        printf("j\n");
        X[i][j] = (int *) malloc(sizeof(int) * n);
  }
}

When you are allocating memory for Triple pointer first you need to allocate memory n double pointers. 首先为三指针分配内存时,需要分配内存n双指针。

int ***X = (int ***) malloc(sizeof(int**) * n); // Not sizeof(int)

Then for that double pointer you need to allocate memory for n single pointers 然后对于那个双指针,你需要为n单指针分配内存

for(i = 0; i < n; i++)
  X[i] = (int **) malloc(sizeof(int*) * n);

For that single pointers you need to allocate memory finally 对于那个单指针,你需要最终分配内存

for(i = 0; i < n; i++)
 for(j = 0; j < n; j++)
        X[i][j] = (int *) malloc(sizeof(int) * n);

This is the way of allocation! 这是分配的方式!


Though a bit more work, it is arguably more straight-forward to use the size of the target pointer dereferenced than coding the type in the sizeof() operator. 虽然稍微有点工作,但是使用取消引用的目标指针的大小比在sizeof()运算符中编码类型更为直截了当。 See below, including the advised removal of malloc() casts in C programs. 请参阅下文, 包括建议删除C程序中的malloc()强制转换。

int ***X = malloc(sizeof(*X) * n);
for (i = 0; i < n; i++)
{
    printf("h\n");
    X[i] = malloc(sizeof(*(X[i])) * n);
    for (j = 0; j < n; j++)
    {
        printf("j\n");
        X[i][j] = malloc(sizeof(*(X[i][j])) * n);
    }
}

Note the only place you see an actual type in this is int ***X . 请注意,您在int ***X看到的实际类型的唯一位置是int ***X Everything else is based on that initial declaration. 其他一切都基于该初始声明。 Why is this arguably "better"? 为什么这可以说“更好”? Ex: To change this entire thing to a 3D matrix of double would require changing one line : double ***X = ... 例如:要将整个事物更改为double的3D矩阵,则需要更改一行double ***X = ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM