Samsung Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
Good Question..
But as far as I know, return value is only the compiler check. It has nothing to do with the call stack...
Infact call stack has space reserved for :-
1> All function parameters.
2> All Variables within that scope
3> Any Variables which are used to call for another subroutine.
I feel its a tricky question,and even if you do something stupid as this.. Your code will not compile " Function should return a Value " or " Parameter Mismatched " in case of C / C++ . So call stack itself will not generate if the code is not able to compile.
Yes, in C++ this might no even compile, but in standard gcc that we use, this will surely compiles and gives result?.
I did some reading on this over google, and figured out, my previous answer to him that this is undefined was correct.
Yep. I wasn't sure about the answer either when I read this question, though I suspected it was undefined. But we seem to be reaching a consensus in this thread that it's UB.
I might note that -Wall would almost certainly catch this, and you might score points with your interviewer for mentioning that you compile with -Wall.
think of what will happen when a value is returned :
the value returned with be saved in one or two register or stack .
if register is used , say R0 on ARM processor, with your code , It will be the value last time used , I guess it is some value in we used in printf for crap.
so %d could be any thing.
As far as i Know printf function always return the length of the string(C Programing by Ritchie), so if we dont return anything from the function then it takes the return type of the last statement which is length of string in fun(), on printing it in main will print the length...also main cant return void in gcc,g++ and most compilers but it hardly matters as it has to do nothing with the question.
The return type of printf function can be checked using nested printf in the main.
When a function call is made, a stack frame is created. As part of this frame, space for return value is also allocated. So, if nothing is explicitly returned; this space is never filled and hence whatever be in that stack at that memory location gets considered as return value.
Thanks,
Laxmi
Hey Laxmi,
thnx for pointing it out.
But, are u sure, do we have a space for return value on the stack for a function??
If I am correct, we don't have return value stored on stack, rather the value of EIP register is considered as return value for the function.
I might be wrong, but I am not convinced with your opinion.
I tried it just on my unix, and saw the value continues to be same for all instances of the program??
Also, gdb never showed any place where the return value is stored.
The usual x86-32 calling convention is to use EAX as the return value, so for many compilers, printf will store 5 there and fun() will implicitly return 5 as well. The return value is not required by standard to be defined. Also, compilers are not required to accept void main().
(Note that EAX is a caller save, so no store need happen in fun after printf is called.)
From WikiPedia: h t t p://en.wikipedia.org/wiki/Call_stack
"Returning from the called function will pop the top frame off of the stack, perhaps leaving a return value."
Note that you have to search for 'return value' ('return address' is another data we store in call stack frame).
Thanks,
Laxmi
It's not guaranteed this behavior due to certain optimizations.
For example, on my machine compiled with gcc 4.5 with -O3 option gives 0.
That's because f is inlined and in the absence of a return value in f, 0 is pushed as second argument to printf instead of eax, as illustrated in assembler code below
...
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
movl $.LC0, (%esp)
call printf
movl $0, 4(%esp) ;<-- here
movl $.LC1, (%esp)
call printf
leave
ret
<pre lang="" line="1" title="CodeMonkey18673" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}jhhjgjkgvjvv
</pre><pre title="CodeMonkey18673" input="yes">
bbmb</pre>
the fun()
- karthikeyan February 23, 2012returns the how many letters processed in printf . result is 5 including \n.