jack: (Default)
[personal profile] jack
In 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?

Date: 2016-06-06 01:30 pm (UTC)
seekingferret: Two warning signs one above the other. 1) Falling Rocks. 2) Falling Rocs. (Default)
From: [personal profile] seekingferret
I'm not much of a programmer, but my understanding was that (a) was the primary concern, that as C/C++ does not require its compilers to check whether a variable is initialized, the variable will have the value of whatever its memory location previously had, and this can obviously have all sorts of unexpected results. But obviously some C compilers DO check if variables are initialized, despite it not being required (I think?) so I guess (b) and (c) are possible as well as (d)the compiler refuses to compile until you override or fix the problem.

I could be totally wrong about all of this, though.

Date: 2016-06-06 02:48 pm (UTC)
damerell: (computers)
From: [personal profile] damerell
I think the most likely outcomes are a) variable assumes random value and d) compiler notices and refuses to compile.

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.

Date: 2016-06-06 04:55 pm (UTC)
gerald_duck: (duck and computer)
From: [personal profile] gerald_duck
What's most likely is that you'll get undefined behaviour. That's not the same thing as an undefined value, so straddles the line between your options a and c.

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.