determining return address of a function
- From: "Madhav" <madhav.kelkar@xxxxxxxxx>
- Date: 27 Dec 2005 03:13:20 -0800
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.
.
- Prev by Date: HCI over USB device pl2303
- Next by Date: Help with File
- Previous by thread: HCI over USB device pl2303
- Next by thread: Re: determining return address of a function
- Index(es):
Relevant Pages
|