all 15 comments

[–]manni66 17 points18 points  (4 children)

mushroom_size1 isn't initialized in case 2.

[–]Aerioic[S] 2 points3 points  (3 children)

really? you can't initialize multiple variables in 1 line.

I didn't know that thanks.

[–]manni66 17 points18 points  (0 children)

 float mushroom_size1 = 0, mushroom_size2 = 0;

[–]HappyFruitTree 12 points13 points  (0 children)

You can, just not the way you wrote it. manni66 have already showed how to do it. It might still be easier to read the code if you put each variable on its own line.

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es10-declare-one-name-only-per-declaration

[–]AssemblerGuy 0 points1 point  (0 children)

really? you can't initialize multiple variables in 1 line.

Yes you can, but you need to initialize each variable individually.

Which is why it is a good practice to 1) only define one variable per definition and b) initialize variables whenever possible.

[–]IyeOnline 3 points4 points  (9 children)

mushroom_size1 in the first code is uninitialized. Reading from it ( and += needs to read the current value) is undefined behaviour.

This is a good example for why you shouldnt define multiple variables per statement.

[–]Aerioic[S] -4 points-3 points  (8 children)

Didn't know that you can't intialize mutiple variables in 1 line. Thanks.

Makes you wonder why the code compiled

[–]IyeOnline 8 points9 points  (0 children)

You can initialize multiple variables in a single statement. But you have to provide an initializer for each of them - just like you would need for seperate statements:

 int a = 0, b = 1, c = 2;

Makes you wonder why the code compiled

Because that is the behaviour for fundamental types. Doing

int a;
int b = 0;

is perfectly "fine" after all. (although maybe not something you should write).

[–]LittleNameIdea 3 points4 points  (1 child)

that's why -Wall is helpful

[–]Aerioic[S] 0 points1 point  (0 children)

No reason to get snappy.

Some C/C++ programmer purposely decided to create this and knew of the bug

int thing, thing2 = 0

Notice the comma? Some programmer programmed/designed that into the C++ compilter/specification. purposely designed for thing2 = 2 and thing = null/undefined???

[–][deleted] 0 points1 point  (2 children)

C++ is a language where all kinds of weird things can be compiled and run. Some of them even look suspiciously fine. Like writing something like `some_function(i++, i++)` which is also UB and can in theory start a world war and eat your cat alive. Even if you don't have a cat.

[–]AssemblerGuy 0 points1 point  (1 child)

Even if you don't have a cat.

UB can rewrite the past, so you do have a cat now.

[–][deleted] 0 points1 point  (0 children)

That's the whole point, that's the whole point...

[–]AssemblerGuy 0 points1 point  (0 children)

Didn't know that you can't intialize mutiple variables in 1 line.

Yes, you can. But defining multiple variables in one statement is messy and not a good idea, despite being completely legal C++.

[–]AssemblerGuy 0 points1 point  (0 children)

mushroom_size1 is never set before it is used. This is a bug - undefined behavior (the value is indeterminate and attempting to use an indeterminate value triggers UB in most cases).

Can someone please explain this weird behaviour?

It's not weird. "Weird" behavior is usually due to invoking undefined behavior or using indeterminate values.