I agree with Andrew's answer. I just want to add a few details where I
think it might have been misleading.
Andrew Haley wrote:
RANJITH KUMAR KASUKURTHI wrote:
3) The location where that return address is stored on stack may vary
depending upon number of arguments passed to the function, or number of
local variables inside the function, or whether the function is returning
a value or not........
Not usually, no. On those machines that pass the return address on the stack,
that address is on the top.
From the point of view of code running inside a function, the location
of the return address typically does vary with the number of local
variables. It does not vary with the number of arguments.
How __builtin_return_address() function knows where exactly the return
address is stored on stack????
The compiler knows where everything is: the return address is nothing
special.
Often the compiler can't know where "everything" (relevant to the
current stack frame) is, because it often can't know how many arguments
were passed.
But (as you indicated) it always knows where the return address is. If
it didn't, then it couldn't even generate correct code for the function
to return.
Because of the flexibility the compiler has with register use, it
typically would be impossible for a something like
__builtin_return_address() to be fully defined by a header file. The
compiler can know all about the stack usage for saved registers and
local variables, etc. so at any point in a function it knows where the
return address is. But code in a header file can't know that much.