2d array arithmetic operation

Hi,
I'm porting some old c++ code, and the original has one line which I cannot work out:

if(abs(a[0] - a[1]) < min_val) i++;

where a is declared as static int a[2][2] and all four values a set in code previous to the above line. My query is: what is a[0] - a[1] actually doing?
When I first break the c++ code the array has the values:
a[0][0]=-7
a[0][1]=-20
a[1][0]=-9
a[1][1]=0
with a result of: a[0] - a[1] = -2
If I manually set everything to 0 I get -2
If I set any of the elements to apparently any number I get -2
If if change the declaration to int a[2][3] I get -3.
If if change the declaration to int a[2][4] I get -4.

I let the code run with a break point if abs(a[0] - a[1] is anything other than -2 and it runs through, hitting this line several hundred thousand times, and it never hits the break point.

Any help in understanding what a[0] - a[1] is doing would be appreciated.
Cheers
Phil.
You are taking the difference of two pointers, so if the array is stored contiguously with C-standard ordering it will tell you the number of elements in the second dimension of the array (or minus it, if you do a[0] - a[1]).

I've switched it to a[1] - a[0] in the example below. Seems more natural , to me anyway. Change the second dimension of the array and watch what happens.

1
2
3
4
5
6
7
#include <iostream>

int main()
{
   int a[2][2];
   std::cout << ( a[1] - a[0] );   // gives 2
}


Maybe it's a way of finding out deep inside the code what was specified right at the beginning, but I think it would be clearer for the next poor devil who has to revise the code if you simply stored the second dimension of the array in an accessible const variable. Or use vector<vector<int>> ...
Last edited on
Thanks, yeah I'd initially thought that, but for the life of me I couldn't figure out why there'd need be a check for changed dimensions. In the five lines between the array declaration and this dimension check there is no code that could possibly alter the array dimensions, nor is 'min_val' changed from its initial value of 10.

I thought there may be some sparsely documented c++ feature when using arithmetic on 2d arrays that does something esoteric with the elements of the lower vectors in the array.

I suspect that this check is left over from previous versions where min_val may be changed or the higher vector of the array increased beyond 9, that code being removed in latter version, but the check remaining. For now I'll comment out the check, and always increment i and see how badly the code breaks.

Cheers
Phil.

that sounds right. If the array is not passed in (and therefore could be some other array than the 2x2) this is either left over from another era or excessively paranoid.
It looks like a bug to me. If the coder really wanted the size of the second dimension, they would have written a[1]-a[0] and not used the abs().

What is the variable i used for? Maybe that will give you a sense of what this line is supposed to do.

And don't tell me "it's not a bug because the code works." It's quite common for working programs to have bugs. Sometimes they have lots of bugs.
Topic archived. No new replies allowed.