determining return address of a function



Hi All,

I recently came across a gnu info manual describing how to use
the function __builtin_return_address to get the address of caller
within a function. Here is my program using that function:
1 #include<stdio.h>
2 #include<unistd.h>
3
4
5 int test1(void);
6 int test2(void);
7
8 int main(void)
9 {
10 test1();
11 }
12
13 int test1(void)
14 {
15 test2();
16 }
17
18 int test2(void)
19 {
20 printf("0th function (this function test2) %p\n",
__builtin_return_address(0));
21 printf("Test2=%p\n",&test2);
22 printf("1st function ( function test1)
%p\n",__builtin_return_address(1));
23 printf("Test1=%p\n",(void *)&test1);
24 printf("2th function (this function main)
%p\n",__builtin_return_address(2));
25 printf("main=%p\n",(void *)&main);
26 }
27
28


And here is the output:

[mkelkar@adrastea cthreads]$ gcc return_addr.c
[mkelkar@adrastea cthreads]$ ./a.out
0th function (this function test2) 0x804836a
Test2=0x804836c
1st function ( function test1) 0x804835d
Test1=0x804835f
2th function (this function main) 0xb74b7748
main=0x8048348

I wonder why the addresses printed are different in the printfs above
and below for each function even when I am using the address of the
same function. Please tell me what I am doing wrong here.

Thanks,
Madhav.

.



Relevant Pages

  • [ncurses] cat xxx |more
    ... int main ... int ch, prev, row, col; ... Works something like a linux cat. ... But this program is limited by *term window size. ...
    (comp.unix.programmer)
  • Re: 16x16Matrix
    ... assume int, but you can pick any other type (including the ones you ... then other replies in the thread might. ... Vladimir ... Prev by Date: ...
    (comp.lang.c)
  • Re: A question about reference in Python.
    ... This is really really really pointless code and a really really pointless ... Python is a superior dynamic programming ... self.prev = prev ... int *next; ...
    (comp.lang.python)
  • Re: A question about reference in Python.
    ... This is really really really pointless code and a really really pointless ... Python is a superior dynamic programming ... self.prev = prev ... int *next; ...
    (comp.lang.python)
  • Re: algorithm to identify number from 0-16384 having at most 4 1s in its binary number
    ... It's probably worth using the well-known algorithm for counting ... the number of bits set in a word (and shortcircuiting the thing ... int main ... Prev by Date: ...
    (sci.math)