0

Compiler Explorer Example: https://godbolt.org/z/AEv4Ci

This code

int main() {
  const int Size = 16; <-- const
  int arr1[Size];
  int arr2[Size];
  int arr3[Size];
  for (auto I = 0; I < Size; ++I) {
    arr3[I] = arr1[I] + arr2[I];
  }
  return arr3[Size - 1];
}

returns 0

but this code:

int main() {
  int Size = 16; // <-- not const
  int arr1[Size];
  int arr2[Size];
  int arr3[Size];
  for (auto I = 0; I < Size; ++I) {
    arr3[I] = arr1[I] + arr2[I];
  }
  return arr3[Size - 1];
}

returns a random number

Jarod42
  • 203,559
  • 14
  • 181
  • 302
rufiplz
  • 3
  • 1
  • Please only tag the language you are compiling it against. You should do that in general and in this particular case C and C++ are indeed different: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – 463035818_is_not_an_ai Feb 12 '20 at 16:53
  • 1
    Choose language, VLA is allowed in C, not in C++. – Jarod42 Feb 12 '20 at 16:53
  • 3
    But reading uninitialized variables is UB for both of your snippets. – Jarod42 Feb 12 '20 at 16:54
  • Both versions are Undefined Behaviour, and the fact that one has different output than output is completely random. – Yksisarvinen Feb 12 '20 at 16:54
  • I'm removing the C tag and leaving C++ since your godbolt example is in C++. If this is in fact a question about C, feel free to edit it the other way. – scohe001 Feb 12 '20 at 16:54
  • `arr3[I] = arr1[I] + arr2[I];` is adding uninitialised variables. That's [undefined behaviour](https://en.cppreference.com/w/cpp/language/ub) and *any* result is acceptable. – Jesper Juhl Feb 12 '20 at 16:56
  • three comments above are correct answers – rleir Feb 12 '20 at 19:06

3 Answers3

1
int Size = 16; // <-- not const
int arr1[Size];

This program is ill-formed in C++, because non-compile-time-constant values may not be used as the size of an array.

arr3[I] = arr1[I] + arr2[I];

The values of arr1[I] and arr2[I] are indeterminate. The behaviour of reading an indeterminate value is undefined.

Why does ... result in ...? ... but this code ... returns a random number

Because in both cases, behaviour of the program is undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

In both case you essentially reading an unitialized data so the program returns garbage values. It just an accident that it returns 0. Try with Size = 14 for example or other number it will also return some random values in the const case too.

Eraklon
  • 4,206
  • 2
  • 13
  • 29
0

The 2nd code

int Size = 16; // <-- not const
int arr1[Size];

is in error in VS C++ (some compilers allow it).

In the code arr1, arr2 are not initialized, arr1[I], arr2[I] , arr3[I] have indeterminate values, assignment “arr3[I] = arr1[I] + arr2[I];” has undefined behavior.

Undefined behavior manifests itself as taking arbitrary values currently stored in arr1[I], arr2[I] and saving result of the “+” in arr3[I]. Concrete values in arr1[I], arr2[I] could change between runs or during the same run.

Val
  • 211
  • 2
  • 4