Connecting Tech Pros Worldwide Help | Site Map

SEGFAULT when declaring input file with ifstream

Newbie
 
Join Date: Feb 2009
Posts: 10
#1: Jun 22 '09
Hi there,
I have to read in from file, and then process it. It looks like this.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. ...
  6. int main()
  7. {
  8. ifstream in_file ("list", ifstream::in);
  9. while (!in_file.eof()){
  10.         in_file.read(buffer,16);
  11.    ...
  12. }
  13. in_file.close();
  14. return 0;
  15. }
  16.  
It compiles OK, but when I run the program, it crashes with SEGFAULT on the line with ifstream in_file ("list", ifstream::in);
GDB shows:
Expand|Select|Wrap|Line Numbers
  1. Program received signal SIGSEGV, Segmentation fault.
  2. main () at HashTest.cpp:14
  3. 14      ifstream in_file ("list", ifstream::in);
and backtrace shows this stupid thing:
Expand|Select|Wrap|Line Numbers
  1. (gdb) backtrace full
  2. #0  main () at HashTest.cpp:14
  3.         buffer = "\bТъщВТ╞\004\b(&╬ЪP\212\004"
  4.         table = Cannot access memory at address 0xffbe2638
  5. (gdb)
What is the reason?:(
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 381
#2: Jun 22 '09

re: SEGFAULT when declaring input file with ifstream


You omitter buffer variable definition; I declared buffer as char[100] and it worked.
zodilla58's Avatar
Expert
 
Join Date: Dec 2006
Posts: 782
#3: Jun 22 '09

re: SEGFAULT when declaring input file with ifstream


Segmentation faults are generally because a pointer of yours is either null or points to some random memory (probably never initialized) or points to memory that was deleted. You are storing read file in buffer which is not initialized and so it is showing error!
Newbie
 
Join Date: Feb 2009
Posts: 10
#4: Jun 22 '09

re: SEGFAULT when declaring input file with ifstream


Quote:

Originally Posted by zodilla58 View Post

Segmentation faults are generally because a pointer of yours is either null or points to some random memory (probably never initialized) or points to memory that was deleted. You are storing read file in buffer which is not initialized and so it is showing error!

But the buffer is already allocated, just after main(){
char buffer[16];
i just omitted that part when posting code snippet. so the reason should be different
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Jun 22 '09

re: SEGFAULT when declaring input file with ifstream


Quote:

Originally Posted by DrSchwartz View Post

But the buffer is already allocated, just after main(){
char buffer[16];
i just omitted that part when posting code snippet. so the reason should be different

You should've posted a compiling, running and small example that exhibits the unwanted behaviour; now you've just shown us an incomplete piece of code that just leaves us guessing.

kind regards,

Jos
Newbie
 
Join Date: Feb 2009
Posts: 10
#6: Jun 22 '09

re: SEGFAULT when declaring input file with ifstream


Thanks for your replies. OK, here is the code. I cannot imagine how I fault in this kind of plain simple program. (Note to avoid possible question: there IS file "list" in the directory of program)
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include "GeneralHashFunctions.h"
  5. using namespace std;
  6. #define CHARBUFSIZE 16
  7. int main()
  8. {
  9. char buffer[CHARBUFSIZE];
  10. int i=0;
  11. ifstream in_file ("list", ifstream::in);
  12. if (in_file.fail() )
  13. {
  14.         cout << "Can't open input file" << endl;
  15.         return -1;
  16. }
  17. while (!in_file.eof()){
  18.         in_file.read(buffer,16);
  19.         std::string key = buffer;
  20.         std::cout<<RSHash(key)<<std::endl;
  21.         i++;
  22. }
  23. in_file.close();
  24. return 0;
  25. }
  26.  
One more thing, when I run program through GDB, it says:
Quote:
warning: Lowest section in system-supplied DSO at 0xffffe000 is .hash at ffffe0b4
what does this mean?
Reply