[LCP]what value does the foo() return?
David Spencer
david.w.spencer at oracle.com
Tue Jan 6 18:44:02 UTC 2004
Greg Black wrote:
>On 2004-01-05, David Spencer wrote:
>
>
>
>>The interviewer could have been probing for how much you knew about what
>>compilers actually do. Undefined is the correct answer; however many
>>compilers I have met return int values from functions in a CPU register,
>>so for those compilers the return value would be whatever is in that
>>particular register when the function returns.
>>
>>
>
>This is so self-evident that it isn't worth saying. Whether the
>particular architecture puts the return values in a register or
>on the stack or in some magic global is utterly beside the
>point. What matters is that, in the example given, using the
>value of a function call where a return statement without an
>expression is executed results in undefined behaviour.
>
>
>
Self-evident - no it isn't. Compiling source to assembler and having a
look at the output is not defined in the Standard yet many programmers
do it for various reasons. The Standard probably doesn't define coffee
breaks yet programmers still do those. I doubt the Standard makes any
mention of the sort of books programmers should read while they're
sitting on the bog. My point is - there is more to life than the
Standard; you can't live by that alone.
Beside the point - no it isn't. The OP was asking why his program
displayed some odd value - my post answered that question. I thought I
had added enough commentary for the reader to distinguish between parts
of my post that followed the standard and those that didn't. As you say
- compilers are free to do whatever they like, that may or may not be
interesting to you but it is what the OP was asking about.
>
>
>>The problem with the answer "Undefined" is
>>that it's a one word answer, and interviewers usually want to get to
>>know a bit more about you.
>>
>>
>
>So if the question is: "Are you male?", you think the answer
>should be more than "Yes" or "No"; when a one word answer is the
>complete answer, then it's perfectly adequate.
>
>
>
If I were asked that in an interview I would be most intrigued why the
interviewer were not able to determine that for himself and would want
to know why. Therefore my answer would run along the lines of "yes, why
do you ask?" ("Can't you tell?" would probably be inappropriate for the
situation.)
>>As a quick test I tried the following program:
>>
>>
>
>This is the surest evidence that you don't understand. As soon
>as somebody tries to make a point about a defined language like
>C by illustrating what happens with some example code, then it's
>clear that they don't get it. The behaviour of correct programs
>is defined by the language and needs no sample code. Incorrect
>programs, e.g., those that exhibit undefined behaviour, are just
>incorrect programs and their behaviour is not interesting.
>
>
>
Ok Greg, what exactly don't I get? The compiler produces defined
behaviour to defined code, outside that it does whatever it likes. Most
compilers I would have thought would do the same thing in the same
situation, i.e. it wouldn't fill in random code every time it
encountered an undefined situation. Of course, this by definition won't
be defined in the Standard, so I would have thought it would be
impossible to answer questions about this area using the Standard.
So for my benefit, how exactly would you satisfy the OP's question
("'Undefined' is not sufficient; the interviewer wants more
information") using just the Standard? I'd really like to see what it
is I'm not getting.
Dave.
More information about the linuxCprogramming
mailing list