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?
a,c, anddare 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.