Segmentation fault
-
QUESTION: Hi! I wrote this code and when i run i get a "Segmentation fault" and it doesn't tell me what line. Can you tell me where this problem is coming, from this code? I entered the value 9070002043 or 9995500000 for the input i get a segmentation fault but when i enter 0003194876 or 0003194875 i get the answer i want. using namespace std; int valid(const char* str); int decode(FILE* fp, const char* str, char* area, char* publisher, char* title); FILE * open(const char filename[]); int registered(FILE* fp, int area); int minNoDigits(FILE* fp, int area); int registered(FILE* fp, int area, int publisher); int close(FILE* fp); int main() { int keepgoing; char isbn[11]; FILE* prefixFile = NULL; char area[6], publisher[8], title[7]; cout = range1 && publisher <= range2) { return 1; } } return 0; } int close(FILE * fp) { fclose(fp); } ANSWER: There is an obvious error in the function int registered(FILE* fp, int area) ; while ( (fscanf(fp, "%d%[^\n]\n", &checkArea, /*** error ***/ &buffer /**/ )) != EOF ) { /* ... */ } should be while ( (fscanf(fp, "%d%[^\n]\n", &checkArea, buffer)) != EOF ) { /* ... */ } ---------- FOLLOW-UP ---------- QUESTION: Hi! i did what you told me and i run the code. I'm still getting the Segmentation fault. Again, When I entered the value 9070002043 or 9995500000 for the input i get a segmentation fault but when i enter 0003194876 or 0003194875 i get the answer i want. Can you help me?
-
Answer:
I can't see anything else obviously causing the problem. My guess would be that the data in the file "prefixRanges.txt" does not match the fscanf format specifiers. The scanf() family of functions have no bounds checking capability - if the string in the file is longer than the buffer size, the characters will overflow into the adjoining memory space leading to undefined behaviour including segfaults. With the %s and %[ conversions, the number of characters read is limited only by where the next whitespace character appears. Invalid input could make your program crash, because input too long would overflow whatever buffer you have provided for it. Ideally, new programmers should avoid the scanf family of functions. In C++, use the safe facilities provided by the iostream library instead. In C, if you have to use the scanf functions, always specify a field width to avoid buffer overflows. The field width you specify should not exceed the number of bytes allocated to your input buffer. For example: char input_buffer[ 32 ] ; cout << "Please enter a string of 20 characters or fewer.\n" ; scanf( "%20s", input_buffer ) ;
Miningco.com Visit the source
Related Q & A:
- How to artificially cause a page fault in linux kernel?Best solution by Stack Overflow
- What exactly is a fault bucket and what causes it?Best solution by Stack Overflow
- What is geographic segmentation?Best solution by Stack Overflow
- Is this the American tourist's fault or the Chinese person's fault?Best solution by Yahoo! Answers
- Did your insurance premium go up after an accident that was your fault?Best solution by 360financialliteracy.org
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.