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 );