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

前端 未结 8 757
感动是毒
感动是毒 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 01:56

    #include 
    #include 
    
    void go(unsigned int addr) {
      (&addr)[-1] = addr;
    }
    
    int sub() {
      static int i;
      if(i++ < 10) printf("Hello %d\n", i);
      else exit(0);
      go((unsigned int)sub);
    }
    
    int main() {
      sub();
    }
    

    Of course, this invokes undefined behavior, is platform-dependent, assumes that code addresses are the same size as int, etc, etc.

提交回复
热议问题