简体   繁体   English

此段故障是什么意思

[英]What does this seg fault mean

I have a segmentation fault when i run my C program and i do not understand it. 我在运行C程序时遇到分段错误,但我不理解。 I am reading the header from a binary file that contains student structs. 我正在从包含学生结构的二进制文件中读取标头。

this is the seg fault i get when i run it with gdb 这是我用gdb运行它时遇到的段错误

Program received signal SIGSEGV, Segmentation fault. 程序收到信号SIGSEGV,分段故障。 0x0804850f in main () at aidb.c:49 49 } main()中的0x0804850f在aidb.c:49 49}

I am under the impression that the segmentation fault is at line 49, however there's only the closing bracket of my main () method at line 49. This is my code, just in case it helps clarify things: 我的印象是分段错误在第49行,但是在第49行只有我的main()方法的右括号。这是我的代码,以防万一它有助于弄清楚事情:

#include<stdio.h>

typedef struct {
    char id_chars[4];
    int file_size;
    int section_table_offset; 
    int section_count;
} Header;


typedef struct {
    int offset;
    int num_entries;
    int type;  // legal value above
} SectionHeader;


    int main(void) {


      FILE *infile = fopen("file.bin", "r");

      Header aidbheader;


        //Reads the aidb file header
       // fread(aidbheader, sizeof(Header),16, infile);
       fread(&aidbheader.id_chars, sizeof(char),4, infile);
       fread(&aidbheader.file_size, sizeof(int),1, infile);
       fread(&aidbheader.section_table_offset, sizeof(int),1, infile);
       fread(&aidbheader.section_count, sizeof(int),1, infile);

       SectionHeader table[4];
       fread(table, sizeof(SectionHeader), 48, infile);


      printf("\nSectionHeader offset: %d \n", table[3].offset);




      return 0;
    }   // this is line 49

You declare table as: 您将table声明为:

SectionHeader table[4];

Then you try to read into table 48 objects of size sizeof(SectionHeader) . 然后,您尝试将大小为sizeof(SectionHeader)对象读入table 48中。

fread(table, sizeof(SectionHeader), 48, infile);

There isn't enough space in table to hold that much data. table没有足够的空间来容纳那么多数据。 Because of that, you write over memory that you are not supposed to. 因此,您会覆盖不应该使用的内存。 That is cause for undefined behavior. 这是导致未定义行为的原因。 In your case, the undefined behavior manifests as segmentation fault when the program returns from main . 在您的情况下,当程序从main返回时,未定义行为表现为分段错误。

You can fix that problem by changing the size of table or changing the fread line. 您可以通过更改table的大小或更改fread行来解决该问题。 Make sure that table has enough space to read the data. 确保该table具有足够的空间来读取数据。

The other error in your code is that the id_chars member of Header is defined as: 您代码中的另一个错误是Headerid_chars成员定义为:

int id_chars[4];

When you read data into it, you are using 当您读取数据时,您正在使用

fread(&aidbheader.id_chars, sizeof(char), 4, infile);

This by itself won't cause segmentation fault. 这本身不会导致分段错误。 It is a symptom of possibly buggy code. 这是可能有错误代码的症状。 Make sure to change the definition of id_chars to 确保将id_chars的定义id_chars

char id_chars[4];
^^^ char  not int

or change the fread line to use: 或更改fread行以使用:

fread(&aidbheader.id_chars, sizeof(int), 4, infile);
                                  ^^^^ int not char

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM