我正在尝试实现一种多态数据结构,例如侵入式链表(我已经知道内核有一个 - 这更多的是学习经验)。
\n问题在于将嵌套结构转换为包含结构会导致gcc发出内存对齐警告。
具体情况如下:
\n// t.c\n#include <stdio.h>\n\nenum OL_TYPE { A, B, C };\n\nstruct base {\n enum OL_TYPE type;\n};\n\nstruct overlay {\n struct base base;\n int i;\n};\n\nstruct overlay2 {\n struct base base;\n float f;\n int i;\n // double d; // --> adding this causes a memory alignment warning\n};\n\nvoid testf(struct base *base) {\n if (base->type == A) {\n struct overlay *olptr = (struct overlay *)base;\n printf("overlay->i = %d\\n", olptr->i);\n } else\n if (base->type == B) {\n struct …Run Code Online (Sandbox Code Playgroud)