C++ uninitialized variable

Personal homepage: AnAoU.com

When I first learned C/C++, I didn't notice this problem. I just thought that uninitialized variables would be assigned a value of 0 by default by the compiler. Later, I found a lot of problems, which made me very confused, and I haven't studied it. For example, if you don't define a variable, its initial value will sometimes be garbled, and sometimes it will be 0. I didn't care about it at first, but now I pick it up from scratch When I started C++, I found the reason and shared it with you.

Show you a picture:


I think the picture says it well, if the uninitialized variable is a global variable, the compiler will attach a value of 0 by default, as expected. If the uninitialized variable is a local variable, it will be a bunch of gibberish. Take a look at the operation effect:

Global variable:


local variable:


Oh! ! Well, it just collapsed, not as expected, haha.

There is also an initialization method, which is also found by accident on the Internet. Let's take a look:



Well, we use int intNums[10] = {}; to initialize in this way, the array is all 0, but it is not initialized, just to verify the above sentence, garbled.

Well, this is the problem I want to explain. Finally, I wish everyone that there is no bug in programming.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326401603&siteId=291194637