How to jump the program execution to a specific address in C?

前端 未结 8 759
感动是毒
感动是毒 2021-01-02 01:17

I want the program to jump to a specific address in memory and continue execution from that address. I thought about using goto but I don\'t have a label rather

8条回答
  •  独厮守ぢ
    2021-01-02 02:00

    gcc has an extension that allows jumping to an arbitrary address:

    void *ptr = (void *)0x1234567;  // a random memory address
    goto *ptr;                      // jump there -- probably crash
    

    This is pretty much the same as using a function pointer that you set to a fixed value, but it will actually use a jump instruction rather than a call instruction (so the stack won't be modified)

提交回复
热议问题