Why we get garbage value in output of program in C++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why we get garbage value in output of program in C++?

Sometimes we're doing problems and everything would be right even though we got garbage value.

16th Nov 2020, 1:45 PM
Janardan Pandey
Janardan Pandey - avatar
2 Answers
+ 2
A garbage value occurs when you allocate a memory for your variable, but you don't assign a value to your variable. The memory of the variable may still have a value from the previous program. We called the value garbage value. This is an undefined behavior, which is, yeah, the behavior C++ does not define. Such thing will cause ANYTHING to your machine, e.g., crash.
16th Nov 2020, 1:51 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
CarrieForle gives a perfect answer. Just one nitpick -- modern operating systems will never give you "a value from the previous program" since that is a security risk. So all garbage you see is your own.
16th Nov 2020, 2:12 PM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar