struct test {
char a;
int b[];
};
int main() {
struct test c;
struct test* ps = (struct test*)malloc(sizeof(struct test) + 5 * sizeof(int));
printf("%d\n", sizeof(c));
int i = 0;
for (i = 0; i < 5; i++) {
ps->b[i] = i;
}
return 0;
}
程序运行结果是4,可以看到还是出现内存对齐的问题。
进行debug调试之后,观察内存.
可以看到,赋值了之后,malloc自动将结构体对齐部分对齐。
(ps:可能有错误,但是我是小白不太清楚,这里还进行了别的测试,柔性数组只能有一个放在最后,其他方式会报错。)