0

I'm doing C++ tests for my certification exam and I came across this exercise that i don't understand: (the question is what is the output of the following program)

#include <iostream>
using namespace std;
class A {
public :
    float v;
    float set(float v) {
    A::v += 1.0;
    A::v = v+1.0;
    return v;
    }
    float get(float v){
    v +=A::v;
    return v;
    }
};

int main()
{
    A a;
    cout<< a.get(a.set(a.set(0.5)));
    return 0;
}

I expected to have an error on the first line of the set function since A::v was never initialized, but my program compiles and it seems that A::v has value 0 by default.. Could someone please explain why there is no compilation error?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
mynameis
  • 3
  • 1
  • 3
  • Thank you for the link, interesting, but it doesn't answer my question - in the answers people say that primitives are not initialized (which is what I expected too), so that doesn't explain how the first line of the set function is able to compile – mynameis Jun 09 '21 at 19:50
  • 4
    You need to understand that it is unpredictable. It can be zero like in your case, but it can be something else. There is rule. It actually answers your question. If you press the pedal to the metal close your eyes and drive through busy crossing you may crash or not. How to answer the question : why did not I crash? – 0___________ Jun 09 '21 at 19:56
  • this question is part of a test for a certification exam. the output of the program is 2, i ran it multiple times on code::blocks and everytime the result was 2, so it can't be unpredictable. if it were, it couldn't have been a question on the test – mynameis Jun 09 '21 at 19:59
  • 3
    As far as the C++ Standard is concerned, the program has undefined behavior, and that's all we can say about it. It might be that some implementation always gives `2`, or usually gives `2`, or keeps giving `2` until something else on that computer happens to give the program different RAM for its virtual address space, or it waits until the worst possible moment to do something other than output `2`. – aschepler Jun 09 '21 at 20:15
  • I do understand what you are saying, I have the same opinion too. But again, this question is part of a test for the C++ Certification Exam by Pearson Vue. In the full test I found two other questions with similar conditions. It is impossible that such a high prestigious institution would include in their tests questions which have unpredictable answers. This is why I believe there might be something more deep about how this program works that maybe we both don't understand. – mynameis Jun 09 '21 at 20:58
  • 3
    Citation needed on the unimpeachable institutional prestige of Pearson Vue. – Nathan Pierson Jun 09 '21 at 21:01
  • It's not an unpredicatable answer. Uninitialized variables have an unspecified value. This is predicted (better: defined) in the C++ standard. - C++ has a basic "philosophy": Don't pay for what you don't buy. The price for this is a variety of things which can be done although they shouldn't be done without an error message. Other languages would add a safety check (with extra performance impact) but C++ is directed to provide maximum performance. – Scheff's Cat Jun 09 '21 at 21:01
  • 1
    "This is why I believe there might be something more deep about how this program works" - you mean some special case that has this not UB because of some obscure reason? Sounds quite fabricated. In any case, have you considered that you answering with it being UB is actually exactly the answer they might want to hear? I mean, other than that, understanding what the code is supposed to do, ignoring the UB, would be far too easy of a question, right? – Aziuth Jun 09 '21 at 22:10

1 Answers1

1

Like you mentioned, the first line of set used A::v, which was never initialized before. However, that itself doesn't produce an error, it is undefined behavior. What it means is the compiler may initialize it for you, or it might just pickup a random number it sees on the memory, or whatever they are pleased to. The C++ standard doesn't say what needs to happen, so it left the compiler to decide whatever is easy.

However, whatever happens on that line shouldn't matter too much in your code, in most cases. The reason is A::v will be re-assigned to v + 1 on the next line. So it should almost always print 2 at the end.

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
  • 1
    thank you, i got so focused on that first line that i didn't even notice it didn't even influence the final output of the program. it's clearer now – mynameis Jun 10 '21 at 20:16