我在看 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 开发者在 commit 1ea89a402d892b68b193e2e4390d8eb33ed686e7 中加入的。 它原本在文件libiolibioP.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. */
};
所以看起来这个字段最初是为了处理C++兼容性而添加的 关于vtable.
随着时间的推移,为了保持相同的偏移量,这个字段的大小和类型被修改了,因为这个结构中增加了更多的字段。 当前版本的这个结构体中多了一个 int
领域,增加一个 size_t
字段,以及四个额外的指针字段,这就说明了原始版本和当前版本之间的大小差异。