0

My teacher told me that he is studying about compiler and he found out that garbage value in c++ can be expected or calculated with a formula like this =

Range of that datatype - size of datatype = garbage value of that datatype.

For example : for integer it should be -

2,147,483,647 (range of int) - 4 (size of int) = 2,147,483,643

and the next memory for int will be - 4 bit every single time.

I was expecting result to be 2,147,483,643

When I test it myself I didn't work for me This is my code -

#include <iostream>

using namespace std;

int main(){
    int a,c,d;
    cout<<a<<" "<<c<< " " <<d;
}

Output = 4352496 7339908 4352384

What I expected = 2,147,483,643 , 2,147,483,639 , 2,147,483,635

Does this mean my teacher was incorrect? he studied wrong thing? Was he talking about another range and another size? I misunderstand something? Can anyone please clear me.

I search this term on google but couldn't find any solutions. I found this surprising is it even possible to calculate the garbage value?

Arjun Ghimire
  • 301
  • 1
  • 2
  • 8
  • 1
    `c` or `c++` - which one? There's no language called `c++/c`. – user438383 Apr 24 '22 at 14:20
  • 1
    What did you expect to see as an output? – Evg Apr 24 '22 at 14:20
  • 13
    You should switch classes to another teacher. That teacher is not competent enough to teach C++. – Sam Varshavchik Apr 24 '22 at 14:21
  • 3
    First, there's no C/C++ language. C and C++ are very different languages. Your code isn't C, please remove that tag. Anyway using uninitialized variables are undefined behavior in both languages. On many platforms that'll make your program crash – phuclv Apr 24 '22 at 14:21
  • 4
    That sounds like absolute nonsense - an indeterminate value can by definition not be determined – UnholySheep Apr 24 '22 at 14:21
  • 4
    ***he found out that garbage value in c++/c can be expected or calculated with a formula like this = Range of the datatype - size of datatype = garbage value of that datatype i.e integer or any other datatype.*** None of this is true in general. Although we are not supposed to reason on what value or what result you get from undefined behavior, this seems to be a strange result I would not expect. – drescherjm Apr 24 '22 at 14:22
  • 1
    [Uninitialized variable behaviour in C++](https://stackoverflow.com/q/30172416/995714), [What happens to uninitialized variables in C/C++?](https://stackoverflow.com/q/70558153/995714), [(Why) is using an uninitialized variable undefined behavior?](https://stackoverflow.com/q/11962457/995714) – phuclv Apr 24 '22 at 14:23
  • 3
    *"My teacher told me that he read about compiler and he found out that garbage value in c++/c can be expected or calculated with a formula like this"* You may want to get more details on the research done. The standard guarantees none of this and different compilers can yield different results. In fact even different flags passed to the same compiler can change the result. E.g. MSVC in debug configuration actually initializes variables before the actual initialization which allows for some checks by the debugger, but in release configuration that doesn't happen... – fabian Apr 24 '22 at 14:25
  • 7
    Your teacher is talking nonsense. `a`, `c`, and `d` are uninitialised. Accessing their values (necessary to print their values) gives undefined behaviour. Undefined behaviour means that the C++ standard specifies NO constraints on what happens. The possibilities on what can happen are endless - and can mean producing junk values, reformatting your hard drive, printing zeros, or anything else. With most compilers, the output is determined by whatever happened to previously occupy the memory locations now occupied by those variables. Results are not required to be repeatable either. – Peter Apr 24 '22 at 14:30
  • 1
    *My teacher told me that he is studying about compiler* -- Your teacher must have been joking with you, since what they are claiming about predicting values from undefined behavior, is complete nonsense. – PaulMcKenzie Apr 24 '22 at 14:48
  • 2
    You should ask your teacher. Possibly, the teacher was testing your credulity (your *gobemouche quotient*) to see if you'd check it out for yourself and determine that the teacher is incorrect. Alternatively, possibly you did not understand what the teacher was talking about in that context. – Eljay Apr 24 '22 at 14:50

1 Answers1

7

Using uninitialized variables is undefined behavior—there is no guarantee that your output will even reflect a “value” for them at all, and the program may misbehave in any way whatsoever. Certainly there is no formula for any such accidental output.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76