c - FILE 结构中未使用的变量
问题描述
我在看struct _IO_FILE
:
struct _IO_FILE
{
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
/* The following pointers correspond to the C++ streambuf protocol. */
char *_IO_read_ptr; /* Current read pointer */
char *_IO_read_end; /* End of get area. */
char *_IO_read_base; /* Start of putback+get area. */
char *_IO_write_base; /* Start of put area. */
char *_IO_write_ptr; /* Current put pointer. */
char *_IO_write_end; /* End of put area. */
char *_IO_buf_base; /* Start of reserve area. */
char *_IO_buf_end; /* End of reserve area. */
...
void *_freeres_buf;
size_t __pad5;
int _mode;
/* Make sure we don't get into trouble again. */
char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
};
我注意到了这个变量char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
这个变量有什么意义?那条评论“确保我们不会再惹上麻烦”呢?
解决方案
这是一个旧的。1997 年 10 月 13 日,一位 RedHat 开发人员首次使用提交 1ea89a402d892b68b193e2e4390d8eb33ed686e7 添加了该字段。它最初位于文件 libio/libioP.h 中。当时添加了以下代码:
/* We had to extend _IO_FILE but this isn't easily possible without
compatibility problems. So we mimic the C++ way to do this which
especially takes care that the position of the vtable stays the
same. */
struct _IO_FILE_complete
{
struct _IO_FILE_plus plus;
_IO_off64_t _offset;
int _unused2[16]; /* Make sure we don't get into trouble again. */
};
所以看起来这个字段最初是为了处理与 vtable 相关的 C++ 兼容性而添加的。
随着时间的推移,该字段的大小和类型随着更多字段被添加到该结构以保持相同的偏移量而被修改。这个结构的当前版本包含一个附加int
字段、一个附加size_t
字段和四个附加指针字段,这说明了原始版本和当前版本之间的大小差异。
推荐阅读
- javascript - 打字稿构造函数参数。解析错误:意外的保留字“公共”
- angular - Angular ngFor 与 FormGroup 内的数组
- c++ - 在外部类的公共构造函数调用中初始化内部私有类-标准的意图是什么?
- c++ - C++ 接口可以从具有完整实现的类继承吗?
- c# - 'br' 行 #### 位置 ## 的开始标签与 'font' epplus 的结束标签不匹配
- css - CSS - 为什么你不能使用十进制数来表示 HSL 中饱和度或亮度的百分比?
- spring-jms - 如何防止同一微服务的多个实例从 ActiveMQ Artemis 主题中读取同一消息
- c# - 线程完成任务后释放内存
- angular - 在使用 HttpHeaders 发送 POST 请求时在 Ionic 应用程序中获取未经身份验证的 401
- reactjs - 在 Typescript 中导入 React 图像