Segmentation Fault11 caused by istream?

I am still new to C++. I am working on a program to read a set of list. I know a segmentation fault can be caused by accessing an array out of bound, but the compiler points out the fault happens at fin(stream) to read data into a double-type variable, and it still can read successfully when accessing the first part of the list.

Here is main function:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "HuygenDC.hpp"

int main(){
    HuygenDC Huygen;
    string filename;
    bool open_huygne;

    filename="./Layertest.txt";
    open_huygne=Huygen.Open(filename);
}

Here is HuygenDC.hpp:
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
#ifndef HuygenDC_hpp
#define HuygenDC_hpp

#include <iostream>
#include <fstream>

using namespace std;

class Point{
private:
    double asin_angle[2];
    int lat,alt,layer[2];
public:
    void SetPoint(istream &fin,int i);
};

class Layer{
private:
    int num_h,num_pallh=0;
    int *num_ph;
    int **index;
public:
    void SetLayer(istream &fin);
    void SetIndex(int ind,int counter);
    int GetPAll();
};

class HuygenDC{
private:
    Point *p;
    Layer *l;
    int num_pall,num_lall;
public:
    bool Open(string filename);
};
#endif 

Here is HuygenDC.cpp:
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
56
57
58
59
60
61
62
63
64
65
66
67
#include "HuygenDC.hpp"

void Point::SetPoint(istream &fin,int i){
    fin>>lat>>alt;
    if (layer[0]==NULL) {
        layer[0]=i;
        fin>>asin_angle[0];//Segmentation Fault:11 or EXC_BAD_ACCESS(code=2,address:0x100000203)
    }
    else{
        layer[1]=i;
        fin>>asin_angle[1];
    }
}

void Layer::SetLayer(istream &fin){
    fin>>num_pallh;
    fin>>num_h;
    num_ph=new int [num_h];
    index=new int *[num_h];
    for (int j=0; j<num_h; j++) {
        fin>>num_ph[j];
        index[j]=new int [num_ph[j]];
    }
}

void Layer::SetIndex(int ind, int counter){
    for (int i=0; i<num_h; i++) {
        counter=counter-num_ph[i];
        if (counter<=0) {
            index[i][counter+num_ph[i]]=ind;
            break;
        }
    }
}

int Layer::GetPAll(){
    return num_pallh;
}

bool HuygenDC::Open(string filename){
    ifstream fin;
    int index,counter=1;

    fin.open(filename);

    if (!fin){
        return false;
    }

    fin>>num_pall>>num_lall;
    p=new Point [num_pall];
    l=new Layer [num_lall];
    for (int i=0; i<num_lall; i++) {
        l[i].SetLayer(fin);
        for (int j=0; j<l[i].GetPAll(); j++) {
            fin>>index;
            cout<<index<<endl;
            p[index].SetPoint(fin,i);//i=0:read successfully, i=1:fault happen
            l[i].SetIndex(index, counter);
            counter++;
        }
        counter=1;
    }

    fin.close();
    return true;
}

(And for your reference, here is the list I want to read-https://drive.google.com/file/d/0B_r9yjyjAGbaMm1mV1k2bU9vTzg/view?usp=sharing)

Thanks!
Accessing an array out of bounds causes the program behavior to become undefined. Once behavior is undefined, the program can do anything, including:
* Crash immediately.
* Continue to run until termination without any errors or crashes.
* Continue to run until termination without crashing, but producing erroneous results.
* Crash at a later time in execution, at a code location arbitrarily far from the code that corrupted memory. Note that this implies that, in the case of memory corruption, the code location where the program crashed doesn't give any information about the code with the bug.
* Corrupt or delete important files.
* Make demons fly out your nose.

Line 29 looks wrong. If counter == 0, you'll write at index[i][num_ph[i]], which from what I can see it's one past the end of index[i].
Topic archived. No new replies allowed.