简体   繁体   English

当我们取消引用FILE指针时会发生什么?

[英]What happens when we dereference a FILE pointer?

Suppose I have a file pointer 假设我有一个文件指针

FILE* infile = fopen("<somefilepath", "r");

Now when I dereference the file pointer in gdb then I get 现在,当我取消引用gdb中的文件指针时,我得到

print *infile 打印* infile

│$2 = {_flags = -72539000, _IO_read_ptr = 0x0, _IO_read_end = 0x0, │$ 2 = {_flags = -72539000,_IO_read_ptr = 0x0,_IO_read_end = 0x0,
│ _IO_read_base = 0x0, _IO_write_base = 0x0, _IO_write_ptr = 0x0, │_IO_read_base = 0x0,_IO_write_base = 0x0,_IO_write_ptr = 0x0,
│ _IO_write_end = 0x0, _IO_buf_base = 0x0, _IO_buf_end = 0x0, │_IO_write_end = 0x0,_IO_buf_base = 0x0,_IO_buf_end = 0x0,
│ _IO_save_base = 0x0, _IO_backup_base = 0x0, _IO_save_end = 0x0, _markers = 0x0, │_IO_save_base = 0x0,_IO_backup_base = 0x0,_IO_save_end = 0x0,_markers = 0x0,
│ _chain = 0x7ffff7dd41c0 <_IO_2_1_stderr_>, _fileno = 3, _flags2 = 0, │_chain = 0x7ffff7dd41c0 <_IO_2_1_stderr _>,_ fileno = 3,_flags2 = 0,
│ _old_offset = 0, _cur_column = 0, _vtable_offset = 0 '\\000', _shortbuf = "", │_old_offset = 0,_cur_column = 0,_vtable_offset = 0'\\ 000',_ shortbuf =“”,
│ _lock = 0x6020f0, _offset = -1, __pad1 = 0x0, __pad2 = 0x602100, __pad3 = 0x0, │_lock = 0x6020f0,_offset = -1,__ pad1 = 0x0,__ pad2 = 0x602100,__ pad3 = 0x0,
│ __pad4 = 0x0, __pad5 = 0, _mode = 0, _unused2 = '\\000' } │__pad4 = 0x0,__ pad5 = 0,_mode = 0,_unused2 ='\\ 000'}

Can someone help me understand what this means ? 有人可以帮助我理解这是什么意思吗?

FILE *应该被认为是一个不透明的值。取消引用该文件将取决于平台/库(因此不可移植)。

C11 7.21.1/2 describes FILE as C11 7.21.1 / 2将FILE描述为

…an object type capable of recording all the information needed to control a stream, including its file position indicator,a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached; …一种对象类型,能够记录控制流所需的所有信息,包括其文件位置指示符,指向其关联缓冲区的指针(如果有),一个错误指示符,用于记录是否发生了读/写错误,以及结束-of-file指示器,用于记录是否已到达文件末尾;

But doesn't mention specific members. 但没有提及特定成员。 Here is glibc's implementation that you observed (I deleted unused preprocessor branches for clarity): 这是您观察到的glibc的实现(为清楚起见,我删除了未使用的预处理器分支):

struct _IO_FILE {
  int _flags;       /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  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. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
  int _flags2;
  _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */

#define __HAVE_COLUMN /* temporary */
  /* 1+column number of pbase(); 0 is unknown. */
  unsigned short _cur_column;
  signed char _vtable_offset;
  char _shortbuf[1];

  /*  char* _save_gptr;  char* _save_egptr; */

  _IO_lock_t *_lock;

  _IO_off64_t _offset;
  void *__pad1;
  void *__pad2;
  void *__pad3;
  void *__pad4;
  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)];
};

FILE is described in the C standard, section 7.21.1: 在C标准的7.21.1节中描述了FILE

2 The types declared are size_t (described in 7.19); 2声明的类型为size_t (在7.19中描述);

FILE 文件

which is an object type capable of recording all the information needed to control a stream, including its file position indicator, a pointer to its associated buffer (if any), an error indicator that records whether a read/write error has occurred, and an end-of-file indicator that records whether the end of the file has been reached; 这是一种对象类型,能够记录控制流所需的所有信息,包括其文件位置指示符,指向与其关联的缓冲区的指针(如果有), 错误指示符 ,该指示符记录是否发生了读/写错误,以及文件结束指示符,用于记录是否已到达文件末尾; ... ...

In your particular instance, it seems you are using glibc. 在您的特定情况下,似乎您正在使用glibc。 It's implementation of FILE can found in libio.h : FILE的实现可以在libio.h找到:

struct _IO_FILE {
  int _flags;       /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  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. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
#if 0
  int _blksize;
#else
  int _flags2;
#endif
  _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */

#define __HAVE_COLUMN /* temporary */
  /* 1+column number of pbase(); 0 is unknown. */
  unsigned short _cur_column;
  signed char _vtable_offset;
  char _shortbuf[1];

  /*  char* _save_gptr;  char* _save_egptr; */

  _IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

Keep in mind the full implementation spans many files, functions, data structures, etc. 请记住,完整的实现跨越许多文件,函数,数据结构等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们在C中取消引用NULL指针时,操作系统会发生什么? - What happens in OS when we dereference a NULL pointer in C? 当您取消引用后增量C时会发生什么 - What happens when you dereference a postincrement C 如果取消引用 null 指针,在 CPU 级别会发生什么? - What happens at CPU-Level if you dereference a null pointer? 当我们将值重新分配给 char 指针时,内存会发生什么? - What happens with memory when we reassign value to char pointer? 如果我们将 integer 指针指向字符数组的开头并取消引用它会发生什么? - What will happen if we point an integer pointer to the beginning of a character array and dereference it? 在C语言中取消引用静态变量时会发生什么情况? - What exactly happens when you dereference a static variable in C? 在MSP430上,当我取消引用空指针时会发生什么? - On MSP430, what will happen when I dereference a null pointer? C中的链表:将当前节点分配给临时节点指针时会发生什么? - Linked List in C: What happens when we assign the current node to a temporary node pointer? 当我们进行 (char *) 转换以将 integer 数据存储到 char 指针时会发生什么? - What happens when we do (char *) casting to store integer data into char pointer? 当一个指针作为指向另一个函数内部的指针的指针传递时会发生什么? - what happens when a pointer is passed as a pointer to a pointer inside another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM