Interview question: variable initialisation



Yesterday's interview included the following code snippet:

void func (...)
{
int x, y = 12;
......

And then the question required knowledge of what is
now in x and y.

Personally, I consider the above to be pretty poor style
and make little effort in my working life to remember the
rules for bits of syntax that I would never use, and probably
change if I came across it in code I was working on (or at
least add comments to explain).

So, what is in x and y?

is it:
x = y = 12;
x = ?, y = 12
x = 12, y = ?.

I plumped for the second, but am not absolutely sure that
I was right.

So that means that x was a random value from the stack
and thus the operation of the rest of the code is unknown.

Which brings me to the second part of the question (and the
reason why I have posted to the embedded group rather than
a 'c' group), func was actually 'main'. Thus it is pretty certain
that x is going to placed in virgin memory, so can one rely
upon it being 0.

My own understand is that this cannot be guaranteed, but
must say that don't think that I have ever worked on a
system which doesn't initialise all global memory at the start.
Nevertheless ,I answered the question as if x was a random
value, but later questions with un-initialised variables led
me to suspect that the test setter expected them to all
be zero.

So what is the rule in an embedded system, can a user
expect all un-initialised *global* variables to be zero
or is it just that they normally are, but that isn't guaranteed?

Comments?

tim



.



Relevant Pages

  • Re: Interview question: variable initialisation
    ... void func ... system which doesn't initialise all global memory at the start. ... So what is the rule in an embedded system, ... expect all un-initialised *global* variables to be zero ...
    (comp.arch.embedded)
  • Re: Interview question: variable initialisation
    ... void func ... system which doesn't initialise all global memory at the start. ... expect all un-initialised *global* variables to be zero ...
    (comp.arch.embedded)
  • Re: Interview question: variable initialisation
    ... void func ... Typically variables are put into segments, ... There is nothing that stops the implementer to zero all variable segments, ... On a machine with SIMD (Single instruction, ...
    (comp.arch.embedded)