C中灵活长度数组的分配空间在哪里?

inj*_*joy 3 c arrays

假设我有如下结构:

struct line {
       int length;
       char contents[];
};

struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
Run Code Online (Sandbox Code Playgroud)

分配的空间在哪里contents?在堆中还是在即将到来的地址之后length

Bas*_*tch 6

根据contents[]定义,灵活数组位于变量大小的结构内,在length字段之后,因此您正好在malloc它的空间中,所以当然p->contents是坐在您所在的区域malloc内(因此在堆内).