简体   繁体   English

Malloc指向通过引用指向结构数组的指针

[英]Malloc a pointer to a pointer to a structure array by reference

The code below compiles, but immediately crashes for reasons obvious to others, but not to me. 下面的代码编译,但立即崩溃的原因显而易见,但不是我。 I can't seem to get it right, can anyone tell me how to fix this. 我似乎无法做到正确,任何人都可以告诉我如何解决这个问题。

*array_ref[2] = array[0];
*array_ref[3] = array[1];

It crashes on that part everytime. 它每次都在那个部分崩溃。

typedef struct test {
    char *name;
    char *last_name;
} person;



int setName(person ** array, person ***array_ref) {

    *array = malloc (5 * sizeof(person));
    *array_ref= malloc(5 * sizeof(person*));

   array[0]->name = strdup("Bob");
   array[1]->name = strdup("Joseph");
   array[0]->last_name = strdup("Robert");
   array[1]->last_name = strdup("Clark");


*array_ref[2] = array[0];
*array_ref[3] = array[1];


    return 1;
}



int main()
{
    person *array;
    person **array_r;

   setName(&array,&array_r);

    printf("First name is %s %s\n", array[0].name, array[0].last_name);
    printf("Second name is %s %s\n", array_r[3]->name, array_r[3]->last_name);

     while(1) {}
    return 0;
}

Operator [] has higher precedence than unary operator* . Operator []优先级高于一元运算operator* Hence, this: 因此,这:

*array_ref[2] = array[0];
*array_ref[3] = array[1];

actually means: 实际意味着:

*(array_ref[2]) = array[0];
*(array_ref[3]) = array[1];

Types are correct here, which is why it compiles. 类型在这里是正确的,这就是它编译的原因。 But from your code it's clear that your intent actually was: 但是从您的代码中可以清楚地看出您的意图实际上是:

(*array_ref)[2] = array[0];
(*array_ref)[3] = array[1];

So just use parentheses. 所以只需使用括号。

您为array_ref指针分配了空间,但没有为它们指向的内容分配空间。

Try changing the following in setName() 尝试在setName()中更改以下内容

 *array_ref[2] = array[0];
 *array_ref[3] = array[1];

to

*(*array_ref+2) = array[0];
*(*array_ref+3) = array[1];

This works. 这有效。

array[1]->name is your problem. array[1]->name是你的问题。 This should be (*array)[1].name . 这应该是(*array)[1].name Notice how the two aren't equivalent. 注意两者是不相等的。 All the similar uses have the same problem, except for [0] , which accidentally does the right thing. 所有类似的用途都有同样的问题,除了[0] ,这意外地做了正确的事情。

Remember that array , the function parameter, isn't your array, it's a pointer to your array. 请记住, array (函数参数)不是您的数组,它是指向数组的指针。

In functions like this I prefer code like: 在这样的函数中,我更喜欢以下代码:

int setName(person ** out_array, person ***out_array_ref) {
    person* array = malloc(5 * sizeof(person));
    person** array_ref = malloc(5 * sizeof(person*));
    array[0].name = strdup("Bob");
    array[1].name = strdup("Joseph");
    array[0].last_name = strdup("Robert");
    array[1].last_name = strdup("Clark");
    // I'm guessing this was your intent for array_ref, here:
    array_ref[2] = &array[0];
    array_ref[3] = &array[1];

    *out_array = out_array;
    *out_array_ref = array_ref;
    return 1;
}

Note that this catches both array[1]->name as noted by Roger Pate, and *array_ref[2] = array[0] as (almost) noted by Pavel - whose solution (*array_ref)[2] = array[0] assigns from an unallocated person* array[1] - both of which are hard to notice with the extra dereference. 请注意,这会捕获Roger Pate所指出的array[1]->name ,以及*array_ref[2] = array[0]注意到的*array_ref[2] = array[0] - 其解决方案(* array_ref)[2] = array [0 ]从一个未分配的person* array[1]分配 - 这两个都难以注意到额外的解引用。

Of course, I mostly do this because I use C++, and this increases exception safety ;). 当然,我主要这样做是因为我使用C ++,这增加了异常安全性;)。

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

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