I’ve had times where I was going through my code and the docs code step by step to see where they logically differed and found that I was doing all the same things, but my code didn’t work and copy-pasting their code did. Make it make sense!
Let me count the ways it has been for me… Capitalization, using -, not using -, wrong quotes, mismatched quotes, no quotes, reading the command the same wrong way about five times and typing it that way. Well this could take forever to list them all.
I just copy paste first now. That way I learn none of the commands or syntax at all
Been there, found undefined behavior where there should not be any.
Imagine a function that takes a bool param with the following code, but neither branch gets executed:
if (b)
doStuffForTrue();
if (!b)
doStuffForFalse();
In a function that is passed an uninitialized bool parameter, in gcc compiler, both branches can get executed even when b is const.
Reason: uninitialized bool in gcc can have values of a random integer, and while if(b) {} else ({} is guaranteed to execute only one branch, bool evaluations of a bool value take a “shortcut” that only has defined behavior with an initialized bool.
Same code with an uninitialized integer works as expected, btw.
Don’t blame this on gcc or the library/function author - it is 100% user (i.e. programmer) error. Uninitialised memory of any type is undefined behaviour in the C and C++ abstract machine. That means optimising compilers can assume it does not exist.
For example, the compiler could see that your ‘b’ is never initialised. Therefore, using it would be undefined behaviour. So, the optimiser can assume it is never used, and it is as if that code simply does not exist: the behaviour you saw.
I’m not saying that is what happened, nor that it will always happen, but it is a possibility.
I’ve had times where I was going through my code and the docs code step by step to see where they logically differed and found that I was doing all the same things, but my code didn’t work and copy-pasting their code did. Make it make sense!
Let me count the ways it has been for me… Capitalization, using -, not using -, wrong quotes, mismatched quotes, no quotes, reading the command the same wrong way about five times and typing it that way. Well this could take forever to list them all.
I just copy paste first now. That way I learn none of the commands or syntax at all
Been there, found undefined behavior where there should not be any. Imagine a function that takes a bool param with the following code, but neither branch gets executed:
if (b) doStuffForTrue(); if (!b) doStuffForFalse();
In a function that is passed an uninitialized bool parameter, in gcc compiler, both branches can get executed even when b is const. Reason: uninitialized bool in gcc can have values of a random integer, and while if(b) {} else ({} is guaranteed to execute only one branch, bool evaluations of a bool value take a “shortcut” that only has defined behavior with an initialized bool.
Same code with an uninitialized integer works as expected, btw.
Don’t blame this on gcc or the library/function author - it is 100% user (i.e. programmer) error. Uninitialised memory of any type is undefined behaviour in the C and C++ abstract machine. That means optimising compilers can assume it does not exist.
For example, the compiler could see that your ‘b’ is never initialised. Therefore, using it would be undefined behaviour. So, the optimiser can assume it is never used, and it is as if that code simply does not exist: the behaviour you saw.
I’m not saying that is what happened, nor that it will always happen, but it is a possibility.
I find myself saying this about 35 times a day, at nearly every turn, with about everything I interact with.