Reputation: 217
In the following code I get a seg.fault, I don't know why this happens since I think that I'm passing the correct parameters to fopen
Compiling :
gcc -o testloadfile testloadfile.c
Running attempts :
First attempt
./testloadfile "correctme.txt"
Second attempt
sudo ./testloadfile correctme.txt
In the same folder of testloadfile.c
I have a .txt
called correctme.txt
The code is the following
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static void load_array(const char* file_name){
char *read_sentence;
char buffer[2048];
char a[100][100];
int buf_size = 2048;
FILE *fp;
int j = 0, c = 0;
printf("\nLoading data from file...\n");
printf("%s \n", file_name); //prints correctme.txt
fp = fopen(file_name,"r"); //error happens here
printf("This line won't be printed \n");
if(fp == NULL){
fprintf(stderr,"main: unable to open the file");
exit(EXIT_FAILURE);
}
read_sentence = malloc((2048+1)*sizeof(char));
read_sentence = fgets(buffer,buf_size,fp);
for(int i = 0; i < strlen(read_sentence); i++) {
a[j][c++] = read_sentence[i];
if(read_sentence[i] == ' ' || read_sentence[i] == '.' || read_sentence[i] == ',') {
j++;
c = 0;
continue;
}
}
free(read_sentence);
for(int i = 0; i < 100; i++)
printf("%s\n", a[i]);
fclose(fp);
printf("\nData loaded\n");
}
int main(int argc, char const *argv[]) {
if(argc < 2) {
printf("Usage: ordered_array_main <file_name>\n");
exit(EXIT_FAILURE);
}
load_array(argv[1]);
}
This is the output for gcc -g testloadfile testloadfile.c
testloadfile: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
testloadfile: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
testloadfile: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o:(.data+0x0): first defined here
testloadfile:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
testloadfile: In function `_start':
(.text+0x0): multiple definition of `_start'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o:(.text+0x0): first defined here
testloadfile: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/tmp/cc0ye2Ms.o: In function `main':
/home/zenoraiser/Scrivania/Università/Secondo Anno/Algoritmi/1718/LAB/Progetto/Esercizio2/testloadfile.c:42: multiple definition of `main'
testloadfile:(.text+0x346): first defined here
/usr/lib/gcc/x86_64-linux-gnu/5/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
testloadfile:(.data+0x10): first defined here
/usr/bin/ld: error in testloadfile(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status
Then i did ulimit -c unlimited
After that i run the program with ./testloadfile "correctme.txt"
In the end I used gdb ./bynary core
, this is it's output
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
./bynary: No such file or directory.
/home/zenoraiser/Scrivania/Università/Secondo Anno/Algoritmi/1718/LAB/Progetto/Esercizio2/core: No such file or directory.
(sorry later i'll try to understand how to use the last command)
Upvotes: 1
Views: 1069
Reputation: 1024
The actual seg fault is likely on this line:
free(read_sentence);
It is important to remember that printf()
sends output to stdout
, which is buffered. As such, things you send to it will not necessarily get printed if a seg fault occurs.
As for the seg fault itself, let's look at these lines here:
read_sentence = malloc((2048+1)*sizeof(char));
read_sentence = fgets(buffer,buf_size,fp);
It looks like you think you are allocating read_sentence
and then putting the data read from fp
into it (and indeed the code will act like this happened), but this is not true.
Instead, you read the data into buffer
and then set read_sentence
to the address of buffer
.
You then do your operations and we come back to the first line I pointed out:
free(read_sentence);
This is not freeing the memory allocated in your malloc()
call above, but rather buffer
which is on the stack, and thus cannot be free()
-ed. As an additional note, that memory you allocated originally for read_sentence
, is now leaked and you won't be able to free it.
The best solution to this would be to drop read_sentence
(along with the malloc()
and free()
pair) all together and just do your operations inside buffer
instead.
Upvotes: 3