#include <stdio.h>
struct s {
int len;
int free;
char buf[];
};
int main() {
printf("sizeof s is %lu", sizeof(struct s));
};
自己对 c 的认知很浅,最近再看 redis 的源码,遇到一些问题,求指教:
现在不管是否注释掉 char buf[];
这一行,它的 size 都是 8,是 buf 这个 char 指针不占空间吗?
#include <stdio.h>
struct s {
int len;
int free;
char buf[];
};
int main() {
printf("sizeof s is %lu", sizeof(struct s));
};
自己对 c 的认知很浅,最近再看 redis 的源码,遇到一些问题,求指教:
现在不管是否注释掉 char buf[];
这一行,它的 size 都是 8,是 buf 这个 char 指针不占空间吗?
char buf[]
这种写法可以出现在结构体里面,只能放在结构体的最后,意义和char buf[0]
一样,就是占位用的,并不分配空间,可以方便访问后面的内存。