1

Code:

#include <iostream>

using namespace std;

long ar[10];
int arraySize;

long aVeryBigSum(long arr[]) {
    long total = 0;

    for (int x = 0; x < arraySize; x++) {
    
        total += ar[x];
    }
    return total;
}

int main() {
    
    cin >> arraySize;
    for (int x = 0; x < arraySize; x++) {

        cin >> ar[x];   
    }
    cout << aVeryBigSum(ar);

return 0;
}
 

Input:

5
1000000001 1000000002 1000000003 1000000004 1000000005

Visual Studio output:

705032719

Online IED output(correct):

5000000015 

I have no idea what to try in this situation as different compilers are giving me different answer. Assuming its something to do with data type long

Tofuman
  • 23
  • 3
  • Visual Studio's long is always 32-bit. You get signed integer overflow and undefined behavior. Try `long long` type. – 273K Apr 11 '23 at 01:11
  • A long in MSVC is only 32 bits. Even with x64. Same as an int. Use long long – doug Apr 11 '23 at 01:15
  • 1
    long is used incorrectly here. The length is only guaranteed to be 32 bits (but a compiler is allowed to use more), "long - target type will have width of at least 32 bits. long long - target type will have width of at least 64 bits " - https://en.cppreference.com/w/cpp/language/types – Dave S Apr 11 '23 at 01:16
  • See [this](https://stackoverflow.com/a/271092/5282154) which describes msvc and clang long size differences – doug Apr 11 '23 at 01:22
  • I don't know why someone gave you a -1. It's not common knowledge that MSVC's `long` is a different size than everybody else's. – Mark Ransom Apr 11 '23 at 01:32
  • You pass array to your function then simply ignore it and use global array and size, you better pass them. But even better to use `std::vector` here – Slava Apr 11 '23 at 02:27

3 Answers3

5

Your long int is likely a signed 32-bit integer type, which means the largest positive integer it can store is 2,147,483,647, but your sum adds up to 5,000,000,015. Because this is larger, integer overflow has occurred.

Replace the long int type with long long int. Or to make the sizes of the types more explicit, include <cstdint> and use int64_t.

Chris
  • 26,361
  • 5
  • 21
  • 42
1

but the limits of the "long" is -2,147,483,648~2,147,483,647; you can usestd::numeric_limits<long>::max();to get the limit. the value total has exceeded its limits since the third addition,so the outcome was not as your expectation. change the long to "long long" you'll get the correct answer.

long long aVeryBigSum(long arr[]) {
long long total = 0;

for (int x = 0; x < arraySize; x++) {

    total += ar[x];
}
return total;

}

as to other IDEs, maybe they do some optimization?

LEO
  • 1
  • 3
1

this problem is associated with specifical compilers and machines. c++ standard just promises that the length of int is not shorter than short, the length of long is not shorter than int, the length of long long is not shorter than long.

Rosa
  • 11
  • 2
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 16 '23 at 03:48