-1

I have the following code:

cin >> r >> c;
char map[c][r];
for(int i = 0; i < r; i++)
{
    string line;
    cin >> line;
    for (int j = 0; j < c; ++j) {
        map[j][i] = line[j];
    }
}


int n;
cin >> n;
//cout << "first " << endl << map[0][0] << endl<< map[1][0] << endl<< map[2][0] << endl<< map[3][0] << endl;

pair<pair<int, int>, pair<int,int>> queries[n];
for (int k = 0; k < n; ++k) {
    int r1, c1, r2, c2;
    cin >> r1 >> c1 >> r2 >> c2;
    queries[n] = make_pair(make_pair(r1-1, c1-1), make_pair(r2-1, c2-1));
}
cout << "test" << endl;

I am experiencing a really weird issue, where my map array seems to lose all its data after the second for-loop. I have breakpoints on the lines "int n;" and "cout << "test << endl;"

On the first breakpoint, my char array is filled with the values i expect, but however on the second breakpoint, all the values in the char array is '\000'.

What could be causing this?

Sofus Øvretveit
  • 323
  • 1
  • 3
  • 10
  • No idea, but you realise that `char map[c][r];` is not legal C++? In C++ array bounds must be compile time constants. Try using a vector instead. `... queries[n];` is illegal for the same reason. – john Mar 20 '19 at 08:26
  • You are evidently doing `using namespace std;` but you also use `map` as a variable name. That's "fun things" waiting to happen when `std::map` enters the game. Anyway, if you could turn this into a [mcve] (read: we can throw it into https://godbolt.org, https://wandbox.org/ or similar and it compiles) we can probably help you further. – Max Langhof Mar 20 '19 at 08:33
  • Plus there's no check for out of bounds access on `line[j]`, but none of this explains the problem you do see. – john Mar 20 '19 at 08:34
  • Should that be `queries[k]` instead of `queries[n]`? (I can't really tell what you are trying to do, so I can't say it is wrong). – jww Mar 20 '19 at 08:52

1 Answers1

3

Problem one:

cin >> r >> c;
char map[c][r];

And later:

pair<pair<int, int>, pair<int,int>> queries[n];

These are variable length arrays (VLAs) which are a feature of C not C++. VLAs are not part of the C++ language, although many compilers support this an extension. Instead, you can use std::vector<std::vector<char>> to emulate the behavior.

Problem two:

for(int i = 0; i < r; i++)
{
    string line;
    cin >> line;
    for (int j = 0; j < c; ++j) {
        map[j][i] = line[j];
    }
}

This is undefined behavior since the length of the read string can be smaller than c, but the code accesses c elements regardless of line.length(). It should be:

for(int i = 0; i < r; i++)
{
    string line;
    cin >> line;
    for (int j = 0; j < c; ++j) {
        map[j][i] = j < line.length() ? line[j] : '\0';
    }
}

Problem 3:

Another undefined behavior

pair<pair<int, int>, pair<int,int>> queries[n];  // better use std::vector
for (int k = 0; k < n; ++k) {
   ...
   // Out of bounds queries[n]
   queries[n] = make_pair(make_pair(r1-1, c1-1), make_pair(r2-1, c2-1));

This last one probably trashes your map array. Most likely the out-of-bounds access resides at the same memory location as some of the elements of map.

Michael Veksler
  • 8,217
  • 1
  • 20
  • 33