Memory issues - 2-dim vector

Hello!

Let me begin by saying I am beginner regarding C++. My problem is as follows: I want to create a 2-dim dynamic vector, with phone numbers on row 0, and row 1, 2, 3 and 4 with either 1 or 0 (1 - answered, 0 - not answered).

Here is my 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
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

// 2-dim dynamic array
vector<vector<unsigned int> > grid (5, vector<unsigned int>(1));


void testfunction(); // prototype

int main()
{
    testfunction();
}

void testfunction() // just to vrify that everything is working a-ok
{
    grid[0].push_back(1); // phone numbers on row 0   
    grid[0].push_back(2);
    grid[0].push_back(3);
    grid[0].push_back(4);
    grid[0].push_back(5);
    grid[0].push_back(6);
    grid[0].push_back(7);
    grid[0].push_back(8);

    int cols = grid[0].size();
    int rows = grid.size();
    int i, j;

    for (i = 0; i < rows; ++i)
    {
	for (j = 0; j < cols; ++j)
	    cout << setw (5) << grid[i][j] << ' ';

	cout << endl;
    }

    grid[1][6] = 2; // problem!

    cout << endl;

    for (i = 0; i < rows; ++i)
    {
        for (j = 0; j < cols; ++j)
	    cout << setw (5) << grid[i][j] << ' ';

	cout << endl;
    }
}


I do however get the right output, but in addition I get:

*** glibc detected *** ./test: free(): invalid next size (fast): 0x0000000001eef0d0 ***

And a whole bunch of backtraces and memory maps.

If I remove the commented "problem" line, the output is


0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 33 0 0
0 0 0 0 0 0 33 0 0
0 0 0 0 0 0 33 0 0
0 0 0 0 0 0 49 0 0


On col 7 some random numbers appear. Should I initialize all columns to zero?

Thanks for any help!
vector<vector<unsigned int> > grid (5, vector<unsigned int>(1));
This will create a grid with 5 rows with 1 column each.


1
2
3
4
5
6
7
8
grid[0].push_back(1);
grid[0].push_back(2);
grid[0].push_back(3);
grid[0].push_back(4);
grid[0].push_back(5);
grid[0].push_back(6);
grid[0].push_back(7);
grid[0].push_back(8);
This will add 8 elements to the first row so you know have a grid of 5 rows where the first row has 9 columns and the rest of the rows have 1 column. Not much like a grid is it?

grid[1][6] = 2; This is out of bounds because the second row doesn't have 7 elements.

When you iterate the grid you use the number of columns on the first row for all the other rows but since they have fewer columns you are accessing elements out of bounds.

If you want the grid to be of a certain size you better specify that in the constructor and avoid using push_back. If you don't know the size when you create the grid or you want to resize it later you can use the resize member function in a similar way to the constructor.
Last edited on
Hmm.. That clarified a whole lot. But the thing is, I don't know how many numbers I'm gonna add. It's taken from a file that can vary in size. All I know is that the numbers of rows are const (5). If I cant use push_back in this case, what is it used for? :)
Topic archived. No new replies allowed.