Interview question: variable initialisation
- From: "tim \(back at home\)" <tim_back_home2006@xxxxxxxxxxx>
- Date: Thu, 4 May 2006 10:07:33 +0100
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
.
- Follow-Ups:
- Re: Interview question: variable initialisation
- From: Steve at fivetrees
- Re: Interview question: variable initialisation
- From: Andrew M
- Re: Interview question: variable initialisation
- From: Vadim Barshaw
- Re: Interview question: variable initialisation
- From: Grant Edwards
- Re: Interview question: variable initialisation
- From: John Devereux
- Re: Interview question: variable initialisation
- From: Mark Odell
- Re: Interview question: variable initialisation
- From: Jonathan Kirwan
- Re: Interview question: variable initialisation
- From: Mark Odell
- Re: Interview question: variable initialisation
- From: Tim Wescott
- Re: Interview question: variable initialisation
- Prev by Date: Re: Caches in embedded systems
- Next by Date: Re: cameralink controller
- Previous by thread: Caches in embedded systems
- Next by thread: Re: Interview question: variable initialisation
- Index(es):
Relevant Pages
|