Segmentation errors in simple 2D array

I took an intro C++ course last semester and to get back into it I've been trying to do a simple program that reads a text file and fills an array with the data. I've been getting weird errors. My array originally had 2 columns and the rows depend on how many entries are in the text. When I try filling this it always results in a "Segmentation Error." Just messing around I decided to make the array 3 columns and fill columns 2 and 3. I don't get a segmentation error but my program always ends in "BUS ERROR." Can someone explain what I'm doing wrong? I will eventually need to sort this data but I don't want to go any further without resolving this issue.


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
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

const int COLS = 3;

void fillArray(int array[][COLS], int& count);
void printArray(const int array[][COLS], int count);

int main()
{
	int count = 0;
	int array[][COLS] = {0};
	
	fillArray(array, count);
	printArray(array, count);
	
	return 0;
}

void fillArray(int array[][COLS], int& count)
{
	ifstream file;
	int weight = 0, cost = 0, target = 0;
	string name;
	cin >> name;
	
	file.open(name.c_str());
	file >> target;

	file.ignore(256,' ');
	file >> weight >> cost;

	count = 0; 

	while(file)
	{
		array[count][1] = weight;
		array[count][2] = cost;
		
		file.ignore(256, ' ');
		count++;
		file >> weight >> cost;
	}

	
}

void printArray(const int array[][COLS], int count)
{
	for(int i = 0; i < count; i++)
	{
		cout << "Row" << i + 1 << "Col 1" 
                     << ": " << array[i][1] << endl
		     << "Row" << i + 1 << "Col 2" 
                     << ": " << array[i][2] << endl;
        }
}



Sample input text:
1
2
3
4
5
6
1250
LJS93K       1300       10500
J38ZZ9       700        4750
HJ394L	     200        3250
O1IE82	     75         10250
L938L3       432        4493
Last edited on
Thanks for the [code] tags.

From just a quick glance (and no real study) line 16 is culprit for the OS errors.
int array[][COLS] = {0};
does not reserve any space in the data segment for the array. The compiler accepts it because it is technically valid syntax... but logically you must specify all dimensions in order to reserve space. (When compiling, make sure you turn on all warnings and your CC should complain about stuff like this.)

It looks like you want to have a variable sized array, so that your input file can be any size you like. You can do this strictly with arrays, but it is more grief than you probably want to spend. I suggest you use a vector instead. Essentially it is an automatically managed, variable-length array.
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 <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct record
  {
  int weight;
  int cost;
  };

void fillArray( vector <record> & table );
void printArray( const vector <record> & table );

int main()
  {
  vector <record> table;

  fillArray( table );
  printArray( table );

  return 0;
  }

void fillArray( vector <record> & table )
  {
  record data;

  ...

  while (true)
    {
    // Skip the magic number at the beginning of the line
    file.ignore( 256, ' ' );
    // Get the data we want
    file >> data.weight >> data.cost;

    // We're done if we hit EOF or error
    if (!file) break;

    // Add the data to our array (vector)
    table.push_back( data );
    }
  }

void printArray( const vector <record> & table )
  {
  for (unsigned n = 0; n < table.size(); n++)
    {
    cout << "Row " << (n+1) << "Col 1: "
         << table[ n ].weight << endl
         ...
    }
  }

Hopefully you can follow that OK.
I changed the record into named fields, but if you really want just a numbered array, you can do that too:
1
2
3
4
5
typedef int record[ 2 ];
...
file >> data[ 0 ] >> data[ 1 ];
...
cout << table[ n ][ 0 ] << endl;

[edit] Oh yeah, don't forget that array subscripts begin at zero.
Hope this helps.
Last edited on
Topic archived. No new replies allowed.