Segmentation fault (core dumped)

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<fstream>
#include<sstream>
#include<stdlib.h>
#include<math.h>
#include<iomanip>

using namespace std;

int main()
{
    int NUM_Object,NUM_Medoid;

   
    cout<<"How many menus u have?"<<endl;
    cin>>NUM_Object;

   
    cout<<"How many Medoids(k) do you want?"<<endl;
    cin>>NUM_Medoid;


    ifstream myfile("Nutrient.txt", ios::in);
    int i,j,k,count;
    count = 0;
    float TEMP_num;
    double Object[NUM_Object][5];
    double Medoid[NUM_Medoid][5],Medoid_New[NUM_Medoid][5],SUM_Distance[NUM_Medoid],SUM_Pij[NUM_Medoid],
         SUM_P[NUM_Medoid],SUM_Distance_New[NUM_Medoid],SUM_Pij_New[NUM_Medoid],SUM_P_New[NUM_Medoid];
    long double Distance[NUM_Object][NUM_Object];

    for(i=0;i<NUM_Object;i++)
    {
        for(j=0;j<NUM_Object;j++)
        {
            Distance[i][j] = 0;
        }
    }
    string line;

   if(myfile.is_open())
    {
        while(myfile >> line)
        {
            i=0;
            while(line[i]!='\0')
            {
                if(line[i]==',')
                {
                    line[i]=' ';
                }
                i++;
            }
            string temp(line);
            istringstream iss(temp);

            string word;
            j=0;
            while(iss >> word)
            {
                TEMP_num = atof(word.c_str());
                Object[count][j] = TEMP_num;
                j++;
            }
            count++;
        }
    }

    for(i=0;i<NUM_Object;i++)
    {
        for(j=0;j<5;j++)
        {
            cout<<Object[i][j]<<" ";
        }
        cout<<endl;
    }

    myfile.close();


}


when i try to declare Distance[NUM_Object][NUM_Object](line30) when i run on server, it shows "Segmentation fault (core dumped)" i have no idea whats wrong with my code. Please help.
(NUM_Object = 854)
Last edited on
idk what did i do but it works now! :)
1
2
3
4
    double Object[NUM_Object][5];
    double Medoid[NUM_Medoid][5],Medoid_New[NUM_Medoid][5],SUM_Distance[NUM_Medoid],SUM_Pij[NUM_Medoid],
         SUM_P[NUM_Medoid],SUM_Distance_New[NUM_Medoid],SUM_Pij_New[NUM_Medoid],SUM_P_New[NUM_Medoid];
    long double Distance[NUM_Object][NUM_Object];


These are all illegal array definitions. The size of arrays must be constant in C++. You have a compiler extension turned on that allows it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
       while(myfile >> line)
        {
            i=0;
            while(line[i]!='\0')
            {
                if(line[i]==',')
                {
                    line[i]=' ';
                }
                i++;
            }
            string temp(line);
            istringstream iss(temp);

            string word;
            j=0;
            while(iss >> word)
            {
                TEMP_num = atof(word.c_str());
                Object[count][j] = TEMP_num;
                j++;
            }
            count++;
        }


There is no attempt made to ensure that count and j remain in bounds for array access here.



Last edited on
thank you:)

but that's just part of my code

i need to use all of them
Topic archived. No new replies allowed.