3

私が次のような構造を持っていると仮定します。

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

struct line *thisline = (struct line *) malloc (sizeof (struct line) + this_length);
thisline->length = this_length;

割り当てられたスペースはどこcontentsですか?ヒープ内またはその後の次のアドレス内length

4

3 に答える 3

6

柔軟な配列は、定義により、フィールドのcontents[]後の可変サイズの構造内にあるため、そのためのスペースにいるので、もちろん、あなたが編集したゾーン内(つまりヒープ内)に座っています。lengthmallocp->contentsmalloc

于 2012-10-18T19:51:27.317 に答える
4

両方。ヒープ内thislineの割り当てられたバッファを指しているため、ヒープ内にあります。呼び出しで要求した追加のサイズはmalloc()、 の割り当て領域として機能しますthisline->contents。したがって、 のthisline->contents直後から始まりますthisline->length

于 2012-10-18T19:52:37.397 に答える
0

コンテンツ用に暗黙的に割り当てられたスペースはありません。

struct line foo;
// the size of foo.contents in this case is zero.

常にポインタを使用して参照してください。例えば、

struct line * foo = malloc( sizeof(foo) + 100 * sizeof(char) );
// now foo.contents has space for 100 char's.
于 2012-10-18T19:51:20.390 に答える