c - FILE 结构中未使用的变量

标签 c file struct io stream

我在看 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字段,以及四个额外的指针字段,它们解释了原始版本和当前版本之间的大小差异。

关于c - FILE 结构中未使用的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61525817/

相关文章:

c - 为什么我的字符串的第一个字母不打印?

shell - awk获取下一行列的值并将其添加到shellscript中的当前行

android - 写入和读取 ArrayList<File> 到 android 缓存内存

用于输出通用结构的 C++ 函数

c - Golang C 绑定(bind)类型设计

c - 我们如何在 Linux 中读取 char*

ios - 不可变值类型 [string] 只有名为 append 的可变成员

c - 程序先绕过fgets

c - 在 c 中将图像旋转 x 角度

java - 在与类文件不同的文件夹中查找资源