Geeky Person
Geeky Person

Reputation: 19

Why does segmentation fault occur even if I am just checking an "Out Of Bound" value and not accessing it

bool isValid(int x, int y, vector<vector<int>> &arr)
{

  if (x >= 0 && x <= arr.size() && y >= 0 && y <= arr[0].size() && arr[x][y] == 1)
    return true;

  return false;
}
  1. I am getting segmentation fault on this ( x <= arr.size() and y<=arr[0].size() ) of the code.
  2. Can you guys please explain why I am getting this even if I am not accessing the out of bound value and I am just comparing it.

Upvotes: 0

Views: 140

Answers (3)

Ali Tou
Ali Tou

Reputation: 2205

What if x == 0 and arr.size() == 0?

In this case, you'll fail on accessing arr[0] inside the y <= arr[0].size() expression, because you are trying to access the vector object present in arr[0] while such thing does not exist.

To be more general, you have to note that accessing arr[x][y] is not valid if x == arr.size() or y == arr[x].size(), but you're allowing such situation by x <= arr.size() and y <= arr[0].size() in your conditions. Your function would finally look like something like this:

bool isValid(int x, int y, vector<vector<int>> &arr)
{
  if (x >= 0 && x < arr.size() && y >= 0 && y < arr[x].size() && arr[x][y] == 1)
    return true;

  return false;
}

Upvotes: 1

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122830

Packing too many conditions into one is not always good. Splitting it up in several conditions can help with debugging. Simple code is not always less code.

bool isValid(int x, int y, vector<vector<int>> &arr) {
      // first index is ok?
      if (x < 0) return false; 
      if (x >= arr.size()) return false;
      // only now you can access arr[x]
      if (y < 0) return false;
      if (y >= arr[x].size()) return false;
      // both are ok
      return arr[x][y] == 1;
}

In your code you are checking arr[0] not arr[x], when all inner vectors are of same size you can consider to use a different data structure. Even when you know that all inner vectors of same size you should nevertheless check arr[x] not arr[0]. Also consider to use size_t (it is unsigned) for the indices, then you can remove the checks for <0.

Your code can segfault because the last valid index is size -1 not size and because arr[0] does not necessarily have same number of elements as arr[x].

Upvotes: 1

Bob__
Bob__

Reputation: 12779

why I am getting this even if I am not accessing the out of bound value and I am just comparing it [?]

Consider y <= arr[0].size(), here you are not just comparing, but accessing the first element of arr and retrieving its size. The problem is that arr may be empty, so that arr[0] would be an access out of bounds.

Also, using <= is an off-by-one error, because size() - 1 is the last valid index.

That function could be rewritten like the following

bool isValid(int x, int y, std::vector<std::vector<int>> const& m)
{
    return x >= 0  and  x < m.size()  and
    //                    ^
           y >= 0  and  y < m[x].size()  and
    //                    ^  ^^^              Are all the same size?
           m[x][y] == 1;
}

Upvotes: 0

Related Questions