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?

11
  • 13
    You should switch classes to another teacher. That teacher is not competent enough to teach C++. Commented Apr 24, 2022 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 Commented Apr 24, 2022 at 14:21
  • 4
    That sounds like absolute nonsense - an indeterminate value can by definition not be determined Commented Apr 24, 2022 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. Commented Apr 24, 2022 at 14:22
  • 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. Commented Apr 24, 2022 at 14:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.