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

前端 未结 8 760
感动是毒
感动是毒 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:02

    Inline assembly might be the easiest and most "elegant" solution, although doing this is highly unusual, unless you are writing a debugger or some specialized introspective system.

    Another option might be to declare a pointer to a void function (void (*foo)(void)), then set the pointer to contain your address, and then invoke it:

    void (*foo)(void) = (void (*)())0x12345678;
    foo();
    

    There will be things pushed on the stack since the compiler thinks you are doing a subroutine call, but since you don't care about returning, this might work.

提交回复
热议问题