3

The following function (extracted from a .cpp file) gives two different results (that is, the output buffer int_image is different) if executed on a PC with Visual Studio (cpu Intel i7 running Windows 7) or on my Android phone (P880). The two input buffers im1 and im2, of type int8 (a synonymous of char), are exactly the same (checked), as well as the parameters w and h. I can't understand why this is happening:

void  Compute(int8* im1,
              int8* im2,
              int w, 
              int h,
              int* int_image)
{
  int index = 0;
  int sum;

  for(int i = 0; i<h; i++)
  {
    // reset this column sum
    sum = 0;

    for(int j = 0; j<w; j++)
    {
      int pn;
      int8 v1, v2;
      v1 = im1[index];
      v2 = im2[index];
      pn = v1*v2;
      //pn = ((int)im1[index]) * ((int)im2[index]);
      sum += pn;
      if (i==0)
        int_image[index] = sum;
      else
        int_image[index] = int_image[index - w] + sum;

      index++;
    }
  }
}

Note. The size of char images im1 and im2 can be such that an integer overflow can happen (but i think that this kind of situation is handled equally by the two compilers, but at this point i am not so sure).

Mat
  • 202,337
  • 40
  • 393
  • 406
Marco Masci
  • 818
  • 10
  • 22
  • 1
    The size of `int` can differ on different platforms so it's possible that your `int_image` is an array of 8-byte items on one platform and is an array of 4-byte items on another. – SomeWittyUsername Dec 23 '12 at 12:37
  • On both systems i have: sizeof(int)=4, sizeof(short int)=2, so this can't be the source of the error. – Marco Masci Dec 23 '12 at 12:44
  • 2
    How is the output different? Simply by answering this question, you might see the answer: why does the function execute differently in different environments? – Christian Madsen Dec 23 '12 at 13:20

1 Answers1

2

Found the source of error. I defined int8 as char, believing that by default it was a signed char. Instead, it is unsigned on gcc, while signed on Visual C++. I do not know what the standard says, but i suggest to all programmers out there to use explicitly signed char and unsigned char when defining your own macro for int8 and uint8. Interesting discussion about this on stackoverflow.

Community
  • 1
  • 1
Marco Masci
  • 818
  • 10
  • 22