-2

I am learning to code in C++. I came across a problem that requires making a function to add up very large number and return the sum. I see a mysterious behavior. When I print the number veryBigSum inside the function aVeryBigSum before returning the value, it works properly.

#include <bits/stdc++.h>
#include <vector>

using namespace std;

long long aVeryBigSum(int n, vector <long long> ar) {

    long long veryBigSum;

    for(vector<long long>::iterator it = ar.begin(); it != ar.end(); ++it )
        veryBigSum += *it;

    cout << veryBigSum << "\n"; 
    return veryBigSum;
}

int main() {
    int n;
    cin >> n;
    vector<long long> ar(n);

    for(int ar_i = 0; ar_i < n; ar_i++){
        cin >> ar[ar_i];
    }

    long long result = aVeryBigSum(n, ar);
    cout << result << endl;
    return 0;
}

Input for the above is:

5
1000000001 1000000002 1000000003 1000000004 1000000005

Output for the above is:

5000000015
5000000015

But when I don't print the number inside the aVeryBigSum function, the cout statement inside main displays some garbage value.

#include <bits/stdc++.h>
#include <vector>

using namespace std;

long long aVeryBigSum(int n, vector <long long> ar) {

    long long veryBigSum;

    for(vector<long long>::iterator it = ar.begin(); it != ar.end(); ++it )
        veryBigSum += *it;

    return veryBigSum;
}

int main() {
    int n;
    cin >> n;
    vector<long long> ar(n);

    for(int ar_i = 0; ar_i < n; ar_i++){
        cin >> ar[ar_i];
    }

    long long result = aVeryBigSum(n, ar);
    cout << result << endl;
    return 0;
}

Input for the above is:

5
1000000001 1000000002 1000000003 1000000004 1000000005

Output for the above is:

5004197743

Can someone please explain to me why is this happening? I mean, how does printing a value affect passing of the variable between to function?

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • Initialize `veryBigSum` to `0` before calculating sum. – CinCout Dec 15 '17 at 06:55
  • I suspect you are seeing symptoms of UB since `veryBigSum` is not initialized. – R Sahu Dec 15 '17 at 06:55
  • @CinCout Then is this behavior happening because of compiler optimization? – sohnryang Dec 15 '17 at 06:57
  • 1
    @sohnryang No compiler optimization. This is Undefined Behavior. – CinCout Dec 15 '17 at 06:58
  • A warning about `#include #include using namespace std;` The first line is a g++ internal implementation library that is not intended to be directly included. It includes the entire standard library. That's a lot of stuff you don't need. This also means that the second line does absolutely nothing. has already been included. The third line places everything in the `std` namespace into the global namespace. Now all that stuff you don't need can easily get in your way, and when it does the errors are bizarre and often barely comprehensible. Use with caution. – user4581301 Dec 15 '17 at 06:59
  • @CinCout Then why does the function returns the right result when printed the value? – sohnryang Dec 15 '17 at 07:00
  • 1
    @sohnryang One cannot justify UB. – CinCout Dec 15 '17 at 07:02
  • Quoting from one of my answers: *"As aptly as it is named, it is not defined, meaning you may get the expected result or you may not. What you should do is not rely on such results since they are not well-defined. You may feel lucky for getting expected results, but believe me, it's a trap!"* https://stackoverflow.com/a/39910584/2912665 – CinCout Dec 15 '17 at 07:03
  • Invoking UB is probably the most feared thing a you can do in a program because you might not know the error is there until it blows up a few [billion dollars of rocket ship](https://en.wikipedia.org/wiki/Cluster_(spacecraft)#Launch_failure), [makes you look very bad in front of your boss](https://www.youtube.com/watch?v=73wMnU7xbwE) or steers an airliner full of people into the side of a mountain. Thank god I don't have an example for that one. – user4581301 Dec 15 '17 at 07:08
  • I thought that including should help in initializing everything to 0. But I guess it does not do that. – Anurag Syal Dec 16 '17 at 07:14

1 Answers1

2

You need to initialize your accumulator:

long long veryBigSum = 0;

It is only by chance (undefined behavior) that you are getting the right answer some times.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125