为三重指针分配内存:C [英] Allocating memory for triple pointer: C

查看:121
本文介绍了为三重指针分配内存: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;

当我在Linux上运行此命令时,出现*** glibc detected *** triplePointer: double free or corruption (out): 0x0000000000ea3050 ***错误,我完全不知道它的含义.但是,当我使用-Wall标志在Windows上运行它时,没有任何错误.有人可以帮我找到我的错误所在吗.

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. But when I run it on Windows with the -Wall flag, I get no errors. Can someone perhaps help me to find where my error is at.

此外,我目前正在通过使用语句X[0][0][0] = 14;进行硬编码.有没有一种方法可以用一些随机值填充此三重指针的所有插槽?

Also, I am currently hard coding by having the statement X[0][0][0] = 14;. Is there a way that I can populate all the slots of this triple pointer by some random values?

推荐答案

尝试以下代码-

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);
  }
}

首先要为三指针分配内存时,需要分配内存n双指针.

When you are allocating memory for Triple pointer first you need to allocate memory n double pointers.

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

然后对于该双指针,您需要为n单指针分配内存

Then for that double pointer you need to allocate memory for n single pointers

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);

这是分配方式!

尽管需要更多工作,但使用目标指针取消引用的的大小可以说比直接编码sizeof()运算符的类型更直接.参见下文,,包括在C程序中建议删除的malloc()强制转换.

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. See below, including the advised removal of malloc() casts in C programs.

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);
    }
}

请注意,您在其中看到的实际 type 的唯一位置是int ***X.其他所有内容都基于该初始声明.为什么这可以说是更好"的呢?例如:要将整个内容更改为double的3D矩阵,则需要更改一行:double ***X = ...

Note the only place you see an actual type in this is 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 = ...

这篇关于为三重指针分配内存:C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆