fscanf - segfault - Address 0x0 is not stack'd, malloc'd or (recently) free'd
Hi everybody!
My problem is the following: I would like to run a C code, and I can compile it without errors, but during the run it gives a segfault. The relevant part of the code: Code:
#include<stdio.h> I checked the code with valgrind, the result is here: Code:
--16922-- REDIR: 0x40189e0 (strlen) redirected to 0x3806ce31 (vgPlain_amd64_linux_REDIR_FOR_strlen) It seems it has a problem with the fscanf lines, but the weird thing is that the code runs without any segfault for some input files, while for others gives the error above. The format of the input files are completely the same, except they contain different numbers. Can you help me out? Thank you very much! Cheers, Joe |
Add some code to test whether each fopen actually worked.
Your seg fault during fscanf is caused by an earlier failure of fopen. When fopen fails (for whatever reason) the FILE* is returns is zero. When you pass a zero FILE* to fscanf, it seg faults exactly as you have shown. Quote:
If you post any code for harder issues, please be more careful to include all the relevant code. Quote:
|
OK, here is a little bit more, but I can't paste here the entire code, because it is very long,
and a little bit copyrighted... Anyway, the rest of it is doing some calculations. But what do you mean that seg fault during fscanf is caused by an earlier failure of fopen? Code:
|
Quote:
Otherwise, you can try out this simple program that duplicates exactly the problem you are experiencing with your program: Code:
#include <stdio.h> |
Well, the only difference I can see is the place of *, and the error check, which is commented out.
The "problem" is, that all of the fscanf's work, i.e. I can printf them out to stdout. So, the real question is that how is that possible (fscanf's work, and give error message at the same time)? The input files are parameters, so I can not specify them exactly in the code. Anyway, thanks for your reply! |
Quote:
If you take the code I presented earlier, compile it similar to what you do for your application, and then run it through valgrind, you will see the exact same error being reported as you indicated in your opening post of this thread. Coincidence? I think not. |
OK, then my question is that how can I heck the return value from fscanf() ?
I'm not sure I understand what You try to explain me. |
Quote:
Code:
int val1, val2; |
The part of my first post you need to understand:
Quote:
Quote:
dwhitney67 wrote sample code to demonstrate what I meant and what you should do about it. But you seem to have gotten bogged down in minor details and side issues of that code and missed the main point. Check the value returned by fopen. If it is zero, the fopen has failed. A zero FILE* returned by fopen should not be passed to fscanf. |
Actually, I've already checked the return value of fopen, and it is correct, so, not a zero file* is passed to fscanf, it is sure.
My main problem is: "The "problem" is, that all of the fscanf's work, i.e. I can printf them out to stdout." and: "Your seg fault during fscanf is caused by an earlier failure of fopen." Where? Thanks! |
Quote:
Earlier you indicated that the program works with a certain collection of files, but yet fails when using others. Besides inserting proper error checking statements in your code, you should also verify that you have proper privileges to read/write the files you are dealing with. |
I have tried your sample code, and I understand why it fails.
But my point is, that fopen and fscanf work fine, all the error checking are positive, the code can open the files, the fscanf read them, I can printf them out again, everything seem to be correct. I have read/write privilegies on all of the files. What I do not understand, that how can the fscanf work anf give error at the same time. By the way, if I compile the code with the -O2 option then it gives the error message for ALL input parameter files. I think I'm going to try another method, if there is an another. Thanks! |
Quote:
But that may mean you provided that file name incorrectly just when using Valgrind, but are seeing some unrelated error when you run the same program outside of Valgrind. |
All times are GMT -5. The time now is 03:58 AM. |