0
#include<iostream>
#include<string>
#include <cstring>
using namespace std;

bool b[200][200];
int a[46];
int test_cases;
int n;
int m;
int first;
int second;

int main()
{
  cin>>test_cases;
  while(test_cases--){

    cin>>n;
    cin>>m;

    for (int i=0;i<2*m;i++){
      cin>>a[i];
    }
    for (int j=0;j<m;j++){
      first=a[2*j];
      second=a[2*j+1];
      b[first][second]=true;

    }

  }

  return 0;
}

Hello. Runtime error seems to occur on the last code 'b[first][second]=true;'

I tried couple of changes and i found if i turn 'b[first][second]=true;' into 'b[second][first]=true;' error doesn't occur, which is simply to change the order of indices.

There is not a possibility of "out of range error" because memory size of b is [200][200] and range of results of a[*] is from 0 to 10.

I can't figure out where the problem is coming from and i need help. Thank you.

2
  • 2
    It's generally a good idea to also provide the sample input together with your source code, as it makes reproducing the error easier. Commented Mar 22, 2020 at 4:33
  • Thank you for your reply. Actually i have found the error. The range of m was 0 to 45 and that's why i set the memory size of a as 46. But at the first loop, it takes iteration 2*m times and i had to set memory size of a larger than 2m, which is larger than 46. Commented Mar 22, 2020 at 5:10

1 Answer 1

1

There is not a possibility of "out of range error" because memory size of b is [200][200] and range of results of a[*] is from 0 to 10.

The world is littered with buggy code because people made assumptions like this. The first thing you should do is prove this correct. That's as simple as placing something like:

if (first < 0 || first > 199 || second < 0 || second > 199) {
    cerr << "Violation, first = " << first << ", second = " << second << "\n";
    exit(1);
}

immediately before your line that sets the b[][] element to true.


As an aside, it would also be prudent to check other array accesses as well. Since we don't have your test data, we have no idea what value will be input for n or m but, since those values can result in undefined behaviour (by accessing beyond array bounds), they should also be scrutinised.

If you wanted to be sure that those didn't cause problems, you could dynamically allocate to the correct size as necessary. For example, once you've gotten m from the user:

int *a = new int[m*2];
// Use it as you wish, elements <0..m*2-1> inclusive.
delete [] a;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply :). You are exactly right and the problem was from memory size of a. m is from 0 to 45 and that's why i set the memory size of a as 46. But it had to be larger than 2m like your answer. Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.