Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有以下结构定义:-
struct structure { int a; int array[]; }one;
当数组大小未指定时,如何为上述结构分配内存?
假设 32 位int和 8 位char,sizeof one很可能4。也就是说,array是一个空(零长度)数组。通常,您会动态分配具有灵活数组成员的结构:
int
char
sizeof one
4
array
struct structure *two = malloc(sizeof *two + 32 * sizeof(int));
它在其字段中创建two了一个指向 a 的指针,其中struct structure包含 32 个元素。array
two
struct structure