Uninitialised variable
Jun. 6th, 2016 12:57 pmIn C and C++, you should avoid using an uninitialised variable for several reasons, not least of which, it's undefined behaviour (?) But in practice, what are the relative likelihoods of the (I think?) permitted outcomes:
(a) it being treated as some unknown value
(b) the following code being deleted by the compiler
(c) something even weirder happening?
(a) it being treated as some unknown value
(b) the following code being deleted by the compiler
(c) something even weirder happening?
no subject
Date: 2016-06-06 01:30 pm (UTC)I could be totally wrong about all of this, though.
no subject
Date: 2016-06-06 04:29 pm (UTC)But there's a smaller set of cases where the value is (eg) incremented but not used. And you shouldn't do that, partly because it will be misleading, and partly because even if it doesn't produce weird behaviour now, it might in future with some other compiler version. But if your code is full of other bad things that don't produce bugs right now, I don't know if this one is in the "don't even allow the program to run with this in" category or the "if it seems to work, there's no point singling it out" category.
no subject
Date: 2016-06-06 02:48 pm (UTC)But you are correct that any of your outcomes are possible because in the case of undefined behaviour the compiler is allowed to do anything it likes - b) and c) and making green demons fly out your nose included.
no subject
Date: 2016-06-06 04:55 pm (UTC)For example, if I write: char c; int i = c; f(i); it's entirely possible for f to be passed 227316459 as its argument, even though that's not a value a char can have. Leaving aside that the compiler would complain bitterly about a case that artificial and obvious, then applying option b.