Fscanf segmentation fault issue (I think)

This is a discussion on Fscanf segmentation fault issue (I think) within the C Programming forums, part of the General Programming Boards category; I have my little program that has to read a number of words from different files, insert them into some ...

Fscanf segmentation fault issue (I think)

I have my little program that has to read a number of words from different files, insert them into some skip-lists and do some other stuff afterwards.

I was given 3 tests to check the program's functionality.

For the first two tests, it works splendidly. However, the last one has a rough estimation of a few hundred thousand words in it and the fscanf() function that I use to read the files with makes the program crash after reading about 50.000 words.

Here's where the problem should be located (If the problem IS with the fscanf() function).

Note: English isn't my native language, so some of the variable names may seem strange to you

I would bet good money that fscanf isn't the cause of your problem. You are most likely trying to scan into a block of memory you shouldn't be scanning into. You probably just ran off the end of your array. The chances of you finding an obscure bug in fscanf is as close to zero as you can imagine.

L=realloc(L,(n+1)*sizeof(List*));

You should never use realloc this way. You should assign the return value to a temporary pointer, because if realloc fails here, you've just lost everything you had.

Code:

T = realloc( L, (n+1) * sizeof *L );
if( T )
L = T;

You should also just control your loop with the return value of fscanf instead of bothering with feof. There's a FAQ on that if you care to read it.

First of all, thank you for your quick response. I really appreciate your advise and yeah... I didn't know about that FAQ. However, I'm still having the exact same problem.

I forgot to mention that I also ran the program through valgrind and this is the ouput when it reaches said Segmentation fault:

Code:

==3601== Invalid read of size 2
==3601== at 0x408AADD: __isoc99_fscanf (isoc99_fscanf.c:31)
==3601== by 0x804964E: main (in /home/student/SD/Tema 2/take 2/a.out)
==3601== Address 0x368 is not stack'd, malloc'd or (recently) free'd
==3601==
==3601==
==3601== Process terminating with default action of signal 11 (SIGSEGV)
==3601== Access not within mapped region at address 0x368
==3601== at 0x408AADD: __isoc99_fscanf (isoc99_fscanf.c:31)
==3601== by 0x804964E: main (in /home/student/SD/Tema 2/take 2/a.out)
==3601== If you believe this happened as a result of a stack
==3601== overflow in your program's main thread (unlikely but
==3601== possible), you can try to increase the size of the
==3601== main thread stack using the --main-stacksize= flag.
==3601== The main thread stack size used in this run was 8388608.

First of all, thank you for your quick response. I really appreciate your advise and yeah... I didn't know about that FAQ. However, I'm still having the exact same problem.

I forgot to mention that I also ran the program through valgrind and this is the ouput when it reaches said Segmentation fault:

Code:

==3601== Invalid read of size 2
==3601== at 0x408AADD: __isoc99_fscanf (isoc99_fscanf.c:31)
==3601== by 0x804964E: main (in /home/student/SD/Tema 2/take 2/a.out)
==3601== Address 0x368 is not stack'd, malloc'd or (recently) free'd
==3601==
==3601==
==3601== Process terminating with default action of signal 11 (SIGSEGV)
==3601== Access not within mapped region at address 0x368
==3601== at 0x408AADD: __isoc99_fscanf (isoc99_fscanf.c:31)
==3601== by 0x804964E: main (in /home/student/SD/Tema 2/take 2/a.out)
==3601== If you believe this happened as a result of a stack
==3601== overflow in your program's main thread (unlikely but
==3601== possible), you can try to increase the size of the
==3601== main thread stack using the --main-stacksize= flag.
==3601== The main thread stack size used in this run was 8388608.

Hope it helps...

This is telling you that your aux variable is not properly initialized before it enters the loop.

Well thing is it enters that loop over 50.000 times and then it gives me the error. Also to note that I free my aux variable and allocate another block of memory at the end of each loop... Seemed to work well for tests that contained fewer words.

Also to note that I free my aux variable and allocate another block of memory at the end of each loop...

Why? I mean really, why is it dynamic at all? You are allocating the same amount of space each time through, so why are you freeing it at all? Why aren't you just using a static buffer of 100 characters? Just memset it if you need to clear it out. Oh, and you never actually check to see if malloc fails or not. So that's probably what's happening. After ther 50,000th time you free 100 bytes and try to get some more, something goes wrong. You never actually check to see if anything goes wrong, so you're oddly surprised when it does.

Again I have to ask, why aren't you just using a static array? More importantly, why don't you actually check the return values of all the functions you are using here?