473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

segmentation fault with fgets

Hi all, I'm completely stuck..
I have a program which reads integers (there are 65536 integers) from one file, and adds them into the Bloom filter. And then it reads another file (with a larger set of integers), and checks the existence of each element in the Bloom filter created in the first step. Here is the code.
Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4. #include "GeneralHashFunctions.h"
  5. #include "falsetest.h"
  6. using namespace std;
  7. int main()
  8. {
  9. int outRes=0;
  10. bfilter BF(BLOOMFILTER_NUMOFHASH, BLOOMFILTER_BITS_SIZE, &outRes);
  11. FILE* pns;
  12. pns = fopen ("pns" , "r");
  13. unsigned int key;
  14. char buffer[16];
  15. while (!feof(pns)) {
  16.         fgets (buffer, 16, pns);
  17.         sscanf (buffer, "%u", &key);
  18.         BF.addmember((char*) &key, sizeof(key));
  19. }
  20. fclose(pns);
  21. int counter = 0;
  22. int counter2=0;
  23. FILE* in_file;
  24. in_file = fopen ("list" , "r");
  25. setvbuf (in_file, NULL , _IOFBF , 4096 );
  26. while (!feof(in_file)) {
  27.         counter2++;
  28.         fgets (buffer, 16, in_file);
  29.         sscanf (buffer, "%u", &key);
  30.         if (BF.isMember((char*) &key, sizeof(key)))
  31.                 counter++;
  32. }
  33. BF.~bfilter();
  34. cout<<"Positives= "<<counter<<"\n";
  35. cout<<"Total= "<<counter2<<"\n";
  36. cout<<"False positives= "<<counter-65535<<"\n";
  37. fclose(in_file);
  38. return 0;
  39. }
when I compile and run it, GDB says:
Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
And here is the output of backtrace full:
(gdb) backtrace full
#0 0x00000000 in ?? ()
No symbol table info available.
#1 0xf7d3f144 in __uflow () from /lib/libc.so.6
No symbol table info available.
#2 0xf7d325b6 in _IO_getline_info () from /lib/libc.so.6
No symbol table info available.
#3 0xf7d32501 in _IO_getline () from /lib/libc.so.6
No symbol table info available.
#4 0xf7d313cd in fgets () from /lib/libc.so.6
No symbol table info available.
#5 0x08048d86 in main () at falsetest.cpp:16
outRes = 1
BF = {m_bf = 0x804d008 "", m_numHashFns = 3, m_logSize = 18, m_bfBitSize = 262144, m_initialized = 1, byteSize = 1, m_debug = 0}
pns = (FILE *) 0x804d018
key = 3029799107
buffer = "3029799107\n\0008\215╛Ъ"
counter = 134529012
counter2 = -5468808
in_file = (FILE *) 0x804a0d9
(gdb)
One more thing, I found that it reads up to 371 integers from the first file ('pns), and then segfaults.
Could anyone help me with this problem?
Jul 1 '09 #1
6 8246
JosAH
11,448 Expert 8TB
Can you print out the value of your file stream 'pns' just after you've opened it (just after line #12) and compare it with the value printed by gdb after the crash? Both pointer values should be equal ...

kind regards,

Jos
Jul 1 '09 #2
donbock
2,426 Expert 2GB
Probably not causing the segmentation fault, but ...
what happens at lines 17 and/or 29 if the string can't be decoded by "%u"?
Jul 1 '09 #3
@JosAH
Thanks for reply... Actually, those two are not equal...The value printed by gdb, is on the line 372 of the file 'pns'. And there is nothing unusual in the file pns, just different integer numbers line-by-line.
Line 371: 3029799106
Line 372: 3029799107
Line 373: 3029799108
etc ........
@donbock
No, I very much doubt that this is a problem... as I've already mentioned, the program segfaults on reading line 372 from file 'pns', and there is nothing unusual there.
What makes me wondered is that the second file -'in_file', is actually superset of the file 'pns', i.e. it has all integers that present in 'pns' plus some others. And if I give a few integers manually to the function 'addmember' (instead of reading them from 'pns'), and then check the loop in lines 26-32 runs perfectly!
Jul 2 '09 #4
donbock
2,426 Expert 2GB
@DrSchwartz
I just wanted to point that you still have a problem to solve after you resolve the segmentation faults. Your program relies on the input files being well-formed. That is not a good assumption to make in the real world. For example, suppose your input file contains "123O4" instead of "12304" (letter O instead of zero). Sscanf will cheerfully decode the unsigned integer value 123 without any hint that there was a problem. Real-world software design can spend more effort identifying and dealing with the corner cases than supporting the nominal functionality.
Jul 2 '09 #5
@donbock
I absolutely share your point. But being aware that the 'pns' is generated by another program (written by myself) which writes explicitly integers to the file, I'm afraid the problem is not in this.
Jul 2 '09 #6
JosAH
11,448 Expert 8TB
@DrSchwartz
No, I didn't mean the data content but the value of the pns FILE* itself, as in:

Expand|Select|Wrap|Line Numbers
  1. pns = (FILE *) 0x804d018
  2.  
kind regards,

Jos
Jul 2 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
13
by: N.S. du Toit | last post by:
Just having a bit of trouble programming with C under FreeBSD 5.1 using the gcc compiler. I'm a bit new to C so my apologies if the answer to my question appear obvious :) Basically I've...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
6
by: jan247 | last post by:
hi, im new here... um, this code works except that, at the end of the loop, i always get a "Segmentation fault". Can anyone help me on this? using GNU C: ..... FILE *fp; fp =...
8
by: Piotr S. | last post by:
What's wrong in following program: #include <stdio.h> #include <stdlib.h> char s; int main() { FILE *fp, *fopen(); if(fp = fopen("plik1","r")== NULL) { printf("blad otwarcia"); exit(1);
10
by: Rav | last post by:
I have recently started working on GCC on red Hat 9. I have encountered with some problems that i think should not occur (at least on Turbo C), here they r: Why does the following piece of code...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
0
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
10
by: Cconfused | last post by:
Howdy! I'm mega confused with all this pointer malarky in C and it's resulting in an ever annoying segmentation fault which just won't go away! Essentially the program I am building has a Get()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.