-2

For example if I subtracted an array with size 3 from an array with size 2, it returns 3 no matter what is inside. Why is that?

Ex:

int a[2] = {1,2};
int b[3] = {999,999,999};
cout << a-b;

And the output is 3

Andy Peng
  • 99
  • 1
  • 1
  • 5
  • 8
    Arrays naturally decays to pointers to their first elements. Pointer arithmetic of unrelated pointers lead to undefined behavior. What are you really doing? Please try to create a [mre] to show us. – Some programmer dude Jan 26 '22 at 06:52
  • Hi, sorry my question was a little confusing. I added my test example. I’m just confused on what subtracting 2 arrays does and I haven’t found anything online that talks about it. – Andy Peng Jan 26 '22 at 07:09
  • 1
    There are many ways to write an invalid program. This is one of them. You won't find most of these ways discussed online simply because there are so many. – n. m. could be an AI Jan 26 '22 at 07:11
  • 1
    It subtracts pointers. That would be legal if they were pointers to elements of the same array, but since those are different arrays, the behavior is undefined. – HolyBlackCat Jan 26 '22 at 07:12
  • 2
    While this is really undefined behavior, what *probably* have happened is that the compiler placed the arrays right next to each other in memory. And the result of pointer arithmetic is always in the unit of the base type (so you subtract two pointers to `int`, so the result is in the units of `int` elements). This behavior could not be relied upon to work always or everywhere. – Some programmer dude Jan 26 '22 at 07:16
  • If you had varied the sizes of the arrays and not just their elements, you could have spotted a pattern. (And drawn the wrong conclusions, but you would at least be able to form a hypothesis about where the 3 comes from.) – molbdnilo Jan 26 '22 at 08:44

3 Answers3

1

The first question here that, what is the subtraction of two arrays? If you are mentioning the subtraction of the elements that have the same indexes, you have to make sure that the array's size is equal. Then iterate through both arrays and subtract one to another using loop. The thing you want to achieve results in undefined behavior. You can alternatively use the overloaded '-' operator to subtract two array objects but it is a little bit complicated.

Burak
  • 9
  • 4
0

This simply happens, because your example does substract pointers of int, instead of the vector.

What happens is this:

&a[0] - &b[0]

I assume you try to achieve a vector substraction instead. One approach is to overload the operatror -.

Have a look here, how operator overloading can be done: What are the basic rules and idioms for operator overloading?

Chris G.
  • 816
  • 2
  • 15
  • "One approach is to overload the operatror [sic]-" but how would you do that for an array? – Bathsheba Jan 26 '22 at 07:49
  • Ideally this should be combined with encapsulating the vector data and specific operations into a templated class/struct. For the operator overloading itself look at this example: https://stackoverflow.com/questions/19697444/c-vector-operator-overloading – Chris G. Jan 26 '22 at 07:58
0

it returns 3 no matter what is inside

No, it does not always return 3.

It depends on the compiler/linker/library/platform/many other factors.

For example, on my machine the g++ compiler and the MSVC get two different results. You should never make any assumptions and rely on this behavior.

#include <iostream>

using namespace std;

int main(int argc, char **argv) {
  {
    int a[2] = {1, 2};
    int b[3] = {999, 999, 999};
    cout << a - b << endl;

    cout << static_cast<void *>(a) << ' ' << static_cast<void *>(b) << endl;
  }

  {
    int a[2] = {1, 2};
    int middle_array[2] = {1, 2};
    int b[3] = {999, 999, 999};
    cout << a - b << endl;

    cout << static_cast<void *>(a) << ' ' << static_cast<void *>(b) << endl;
  }
  return 0;
}

With G++

$g++ t3.cpp && ./a.exe
3
0xffffcc28 0xffffcc1c
5
0xffffcc14 0xffffcc00

With MSVC

cl t3.cpp && t3.exe
-6
00000012814FFB00 00000012814FFB18
-8
00000012814FFB08 00000012814FFB28

P.S. For the curious. These are test results of two different machines(g++):

4
0x7ffcdaa00640 0x7ffcdaa00630
8
0x7ffcdaa00620 0x7ffcdaa00600
-2
0x7ffd969a2904 0x7ffd969a290c
-4
0x7ffd969a28fc 0x7ffd969a290c
Zongru Zhan
  • 546
  • 2
  • 8