segmentation fault.

Dear all,

This code tried to read the text file and save in array.

I don't know what's the root cause of "segmentation fault"
in the attached code. Could you please have a look on this?

Thanks a lot in advance!
Junhui


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>

using namespace std;

int main ()
{
    //At first, put all of the items into a vector, then operate the vector.

    ifstream inFile ("../test_7_10lines.tsv");
    //ifstream inFile ("../two_ch.tsv");
   if (!inFile.good() || !inFile.is_open() )
   {
      std::cout << "\nWARNING! Cannot read from file: " <<" ../two_ch.tsv"<< std::endl;
   }
    string line;
    int linenum = 0;
    double time_, vol_, time[5000000], vol[5000000];
    while (getline (inFile, line));
    {
        linenum++;
        istringstream linestream(line);
        if(linenum >= 22)
        {
             string item;
             int itemnum = 0, j = 0;
             j = (linenum - 22)* 4096 + itemnum ;
              while (getline (linestream, item, '       '))
              {
                 itemnum ++;
                 if ( (itemnum % 2) != 0)
                 istringstream(item) >> time_  ;
                 else
                 istringstream(item) >> vol_  ;
                 if (inFile.good())
                 {
                    time[j] = time_;
                    vol[j] = vol_;
                 }
                 else
                 {
                    cerr<<"File read error !"<<endl;
                    exit(111);
                  }
               }//end of while(getline ()) loop
             cout << "time [j] = "<< time[j] << "\tvol [j] = " << vol[j] << endl;
         }//end of if(linenum >=22)
    }//end of while (inFile, line)
    inFile.close();
    return 0;
}
double time_, vol_, time[5000000], vol[5000000];

I'm pretty sure that's way over the size of your stack. Use some heap memory instead.
Hi, firedraco,

Thanks for you comment.
It's solved by using "vector<double> time;"

But I still don't understand how this error happened.
Since the elements number around 4M, but I defined 5M array.
Why over size?
Does the table and end line character (" " and "\n") occupy "place"?

Best regards,
Junhui
He is saying that the program is only given a small amount of stack memory. The problem is not in your use of the array. It isn't a buffer overflow that he is talking about. he is saying that the program simply cannot construct an array of that size on the stack. A vector works because the vector's underlying pointer attribute points to a block of memory that was allocated using heap memory. So even though the vector itself uses stack memory for some of its class attributes it creates a dynamic array using heap memory.
Thanks a lot for your quite clear explanation !
Best !
Topic archived. No new replies allowed.