Are “malloc(sizeof(struct a *))” and “malloc(sizeof(struct a))” the same?

后端 未结 4 2285
鱼传尺愫
鱼传尺愫 2020-11-29 09:41

This question is a continuation of Malloc call crashing, but works elsewhere

I tried the following program and I found it working (i.e. not crashing - and this was m

4条回答
  •  感动是毒
    2020-11-29 10:18

    is it valid to assign that to struct a * instead of struct a ** ?

    Well, technically speaking, it is valid to assign like that, but it is wrong (UB) to dereference such pointer. You don't want to do this.

    valid to access/de-reference the allocated array(n-elements)-of-pointers-to-type-"struct a" using pointer-to-"struct a" ?

    No, undefined behavior.

提交回复
热议问题