M
Mark McIntyre
I think Seebs might have a problem with that.
Heck, roose could offer to help moderate it couldn't he?
=:-0
I think Seebs might have a problem with that.
I wish *I* had thought of that. But still, the answer I gave in that
other thread was also a good one, right? Right? RIGHT?
void fn(void)
{
/* add your code here so that main should print something
* other than 20
*/
}
int main(void)
{
int i = 20;
fn();
printf("%d\n", i);
return 0;
}
Mark said:BTW, I'm only replying to ensure that anyone else reading this thread
will see that you are spouting rubbish.
Mark said:Ever heard of registers? A
*lot* of good compilers will store automatic variables in registers
where possible rather than on the stack.
... but in the case when a stack is used in function calls, wouldn't
these registers again be saved on the stack in preparation for the
call to f()?
I suppose a brilliant compiler would
know that rA (for example) is not going to be changed by the call to f()
but are compilers this smart?
And at that point couldn't a knowledgable programmer, someone who
knows the inner workings of the compiler in question, force the
matter anyway? Or, if 'i' is located in a register, use inline
asm to alter that register.
Like I said before, requires a lot of knowledge about a particular
processor/environment/compiler and is entirely non-portable, but I think
it is probably always 'possible'. However, anyone stupid enough to code
like this should be fired immediately.
Totally unnecissary. I think everyone here can see plainly that Roose
is choking to death on his own dung.
Not to say that Roose is right (I can't even know everything he said
as I killfiled him already), but in the case when a stack is used in
function calls, wouldn't these registers again be saved on the stack
in preparation for the call to f()?
I suppose a brilliant compiler would
know that rA (for example) is not going to be changed by the call to
f() but are compilers this smart?
And at that point couldn't a
knowledgable programmer, someone who knows the inner workings of the
compiler in question, force the matter anyway? Or, if 'i' is located
in a register, use inline asm to alter that register.
Like I said before, requires a lot of knowledge about a particular
processor/environment/compiler and is entirely non-portable, but I
think it is probably always 'possible'.
However, anyone stupid enough
to code like this should be fired immediately.
prashna said:void fn (void);
main ()
{
int i = 20;
fn ();
printf ("%d\n",i);
}
void fn (void)
{
// add ur code only here so that i in main should print OTHER THAN 20
}
Is there any Legal solution for this?
Thanks,
Ashwath
Mark Gordon said:Yes, I completely agree with you.
Aren't you guys completely missing the point of this interview
question?
The interviewer wanted to know if the interviewee (the original
poster) was aware of the dangers of undefined behavior in C so he or
she asked how undefined behavior could reasonably lead to a very
specific case of unexpected behavior.
If any C programmer couldn't answer this question, I would suspect he
might have major problems finding bugs in programs invoking undefined
behavior.
Kelsey Bjarnason said:Didn't see your answer... but his isn't a good one. It says "so that
_main_ should print something other than 20"; this "solution" doesn't meet
the requirements. main still prints 20; it's simply added onto the end of
other, unrelated output.
Mark said:That is not the question as posted by the OP, although it *might* have
been the question as either asked or intended by the interviewer.
Phil Tregoning said:(e-mail address removed) (prashna) wrote in
This is invokes undefined behaviour, but may work on
some implementations (not mine):
void fn(void)
{
"%d\n"[0] = 'i';
}
{
"%d\n"[0] = 'i';
If we go by that logic, then this will work too:
void fn(void) {
int i;
i=i++;
}
void fn (void);
main ()
{
int i = 20;
fn ();
printf ("%d\n",i);
}
void fn (void)
{
// add ur code only here so that i in main should print OTHER THAN 20
}
Is there any Legal solution for this?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.