I have a segmentation fault when i run my C program and i do not understand it. 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
Program received signal SIGSEGV, Segmentation fault. 0x0804850f in main () at 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:
#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
int id_chars[4];-->char id_chars[4];,fread(table, sizeof(SectionHeader), 48, infile);:48-->4- BLUEPIXYreturnstatements) are usually due to overwriting of the function return address, which is often caused by going out of boundaries of local arrays. - Matteo Italia