Stack and pointers

This is a discussion on Stack and pointers within the C Programming forums, part of the General Programming Boards category; I have a simple program here: Code: void main() { int *p; p = (int *)&p + 2; /* what???? ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    4

    Stack and pointers

    I have a simple program here:
    Code:
    void main()
    {
            int *p;
            p = (int *)&p + 2;                              /* what???? */
            (*p) = (int)somewhere_else;
    }
    I found that code on the net, and I'm wondering what that second instruction means. The program is supposed to change the function's return address. I would do the same thing like this:
    Code:
    void main()
    {
            int *p;
            p = p + 2;
            (*p) = (int)somewhere_else;
    }
    Could somebody just explain what that earlier example's 2nd instruction means. There's like &-operator in front of a pointer??? somewhere_else is just a pointer pointing somewhere else.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The only thing I can tell is that you don't get the same result when you do (int *)& to a pointer. You get the address of the memory of the pointer. That address you store it at the same pointer. So the pointer points itself. That is all. Don't know why you want to do this...

    EDIT: Hmm, you generally get a seemingly "random" number when you get the address of the pointer. But just guessing here, the programmer of this code might know that the return address of main is stored 2 memory blocks (each memory block stores one int) away from where p is stored. Why? Don't know, but p is the only local variable so you know, it is possible the program behaves like that. So you store the return variable on p. Then you change the value of the return address to what you want, and I assume main returns there.
    Last edited by C_ntua; 09-19-2008 at 12:45 PM.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    In your rewrite, the initial value of p is undefined, and then you add 2 to it. They won't work the same.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    They're quite different, since the first example takes the address of the pointer (int**), and your second code takes the address stored inside the pointer (which is undefined).
    This code is quite a hack, though. Typically function calls works by first pushing the return address to the stack and then jumping to the function. When returning, the return address is popped off the stack and jumped to again.
    So what this code does is get the address of the local pointer p, which is located on the stack. Then it would add +8 to jump to the place where the return address is stored on the stack. Then it can overwrite that address. So when the function returns, it returns not to the caller function but something else.

    STACK (example):
    Return address (4 bytes) [0x0000000B]
    ...Something else... (4 bytes) [0x00000008]
    Local pointer p (4 bytes) [0x00000004]
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  3. c / asm / stack and pointers
    By Lynux-Penguin in forum Tech Board
    Replies: 9
    Last Post: 08-06-2003, 01:08 PM
  4. Making a Stack using Pointers
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 07-27-2002, 12:51 PM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21