开发者

Printing the return address using format string

I am required to print out the return address stored in the stack for a function? The format I am supposed to follow to print it is

void function() 
{
      int *RetPtr = 0;
      printf("Return address 0x%08x\n",
                 (unsigned int *)(&RetPtr+Return_addr_Offset));
      /*Code goes here*/

}

Can someone please help! I am not supposed to be using inbuilt function. How can I do 开发者_运维百科this?


This smells like homework to me if you can't use the built-in functions.

In any case, there's nothing magic about it being a pointer; you just need to print out a hex number.

Without knowing what functions if any you are allowed to use, it's kind of hard to help. You basically need to work out the eight hex digits then output them.


You need to work out what Return_addr_Offset is supposed to be. RetPtr is allocated on the stack, so by calculating some offset relative to RetPtr's address, you can print out the address of the calling function that was stored in the stack when it called into your function. The actual value of offset you need depends on what computer architecture you're on and the calling convention.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜