Calling a function through its address in memory in c / c++

前端 未结 6 744
天涯浪人
天涯浪人 2020-12-02 13:46

Given knowledge of the prototype of a function and its address in memory, is it possible to call this function from another process or some piece of code that knows nothing

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 13:53

    In most OP, every process has its own memory, so you can't.

    Sample code: a.c:

    #include 
    
    int r() {return 2;}
    int main() {
        printf("%p\n",r);
        while(1);
    }
    

    b.c:

    #include 
    
    int main() {
    int a,(*b)();
    scanf("%p",&b);
    a=b();
    printf("%d\n",a);
    return 0;
    }
    

    this get segmentation fault.

提交回复
热议问题