struct sdshdr {
int len;
int free;
char buf[];
};
struct shshdr *p = malloc(sizeof(struct shshdr));
+---------+----------+-----------------+
p --> | int len | int free | char[] buf 0..n | can be expanded
+---------+----------+-----------------+
struct sdshdr {
int len;
int free;
char *buf;
};
struct shshdr *p = malloc(sizeof(struct shshdr));
+---------+----------+-----------+
p --> | int len | int free | char* buf | cannot be expanded, fixed size
+---------+----------+-----------+
|
+-----------+
| |
+-----------+
在第一種情況下,這是有效的: 「你好,世界」
struct shshdr *p = malloc(sizeof(struct shshdr)+100); // buf is now 100 bytes
...
struct shshdr *q = malloc(sizeof(struct shshdr)+100);
memcpy(q, p, sizeof(struct shshdr) + 100);
聲明'焦炭BUF [] =;'不會導致錯誤。 –