segmentation fault in 2d array. please help!

hi to all!
i'm new to this forum and to c++ too. i'm writing some code to parse a csv file, which fields are then copied in a 2d string array. I cannot be sure every time how big the csv is going to be (how many lines-records it has). All i know is that i have a 47 field-columns csv, with many records-lines. My thought is that if the lines of the csv are more than the declared size of the 2d array, reallocate and add 30 rows to the array. here is the source code:


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
82
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class CVSRow
{
    public:
        
        int flows_len;                  // number of rows to initialize the flows array
        string **flows;                 // flows array
        string **temp;                  // temporary array to copy the flows array if expansion is needed
        int Num_flows;                  // Number of flows parsed

void readNextRow(istream& str)
        {
for (int j=0;j<flows_len;j++){
                                                    
     if (Num_flows >= flows_len) {                   // if expansion is needed
         flows_len = flows_len+30;            // resize
         temp = new string*[47];
            for (int i=0;i<47;i++){
               temp[i] = new string[flows_len];}   // create new bigger array.
                    for (int j=0; j<Num_flows; j++) {
                        for (int i=0;i<47;i++){
                            temp[j][i] = flows[j][i];       // copy values to new array.
                            }
                        }
                
                    for (int i=0;i<47;i++){     // free old array memory.
                        delete [] flows[i];
                    }
                    delete [] flows;
                    flows = temp;             // now flows points to new array.
                        }
      string         line;
      getline(str,line);           // get the first line-row-flow

      stringstream   lineStream(line);
      string         cell;
   for (int i=0;i<47;i++){
        getline(lineStream,cell,',');    // get the comma seperated strings-columns-fields
        flows[j][i]=cell;                 // fill the flows array
                     }
                Num_flows++;            // number of rows-flows parsed ++
            }
}
        CVSRow (){                    // Class Constructor
        flows_len = 500;
        flows = new string *[47];
        for (int i=0;i<47;i++){
        flows[i] = new string[flows_len];}
        Num_flows=0;
        };
};

istream& operator>>(istream& str,CVSRow& data)
{       
    data.readNextRow(str);
    return str;
}

int main()
{
   std::ifstream       file("/home/alex/Desktop/201212111940");
   CVSRow row;

while (file>>row){
       
       for (int j=0;j<row.Num_flows;j++){
       for (int i=0;i<47;i++){
           cout << j << "." << i << ":" << row.flows[j][i]<< "\n";
       }
       }
}
}


i'm using netbeans IDE, on ubuntu12.
when running i get run failed, and the debugger says segmentation fault,no registers. The exact spot that the run stops is the same every time at line 49:
 
flows[j][i]=cell; 

with j=47!

Please any help would be precious! This may be an overflow, but i have searched everywhere and cannot solve it! Excuse me for being a beginner and if the code is a little bit difficult to read!!

thanks in advance

Why is this program attempting to use new string[] instead of vector<string>?
flows = new string *[47];

The exact spot that the run stops is the same every time at line 49:

flows[j][i]=cell;

with j=47!


Valid indices for an array with 47 elements are 0 to 46, so it's not surprising that you run into problems when you use an invalid index into the array. In more general terms, valid indices for an array with size elements are 0 to size-1.
Cubbi (1675)
Why is this program attempting to use new string[] instead of vector<string>?


because later on, i need to have a clear index of the parsed csv fields to create other arrays. For example, i want to create an array whose elements are flows[j][4]+flows[j][5] .

cire (1946)
flows = new string *[47];

The exact spot that the run stops is the same every time at line 49:

flows[j][i]=cell;

with j=47!


Valid indices for an array with 47 elements are 0 to 46, so it's not surprising that you run into problems when you use an invalid index into the array. In more general terms, valid indices for an array with size elements are 0 to size-1.


this is for the i dimension. the inner loop is for 0<=i<47 and the outer loop is for 0<=j<flows_len ,when flows_len=500. so, 47 is valid for the j dimension, it shouldn't stop there
this is for the i dimension. the inner loop is for 0<=i<47 and the outer loop is for 0<=j<flows_len ,when flows_len=500. so, 47 is valid for the j dimension, it shouldn't stop there


In the code I quoted: flows = new string *[47]; the dimension indexed by j is limited to 47 elements (which makes the highest valid index 46.)

You seem to have your dimensions mixed up.

When someone tells you what is wrong with your code, test it instead of immediately saying, "DERRRR... NO IT'S NOT!" Obviously something is wrong with your code or you wouldn't be asking for help. And, obviously it is something that isn't immediately apparent to you or you wouldn't be asking for help.
Topic archived. No new replies allowed.