Tell me, is it possible to somehow transfer the control of a program in C / C ++ to an arbitrary available memory?
Made a test example (mingw, Windows), but does not work, the program crashes when pw3 (5) is called.
#include <iostream> using namespace std; void work1(int value){ cout << "work1 " <<value <<endl; } void work2(int value){ cout << "work2 " <<value <<endl; } void (*pw1)(int) = work1; void (*pw2)(int) = work2; unsigned char data[100]={}; unsigned char *funcData = (unsigned char *)pw1; int main() { cout << "start test programm" << endl; work1(1); work2(2); pw1(3); pw2(4); cout <<(void*)pw1 << endl; cout <<(void*)pw2 << endl; void (*pw3)(int) = (void (*)(int))data; for(int i=0;i<100;i++){ data[i] = funcData[i]; } pw3(5); cout <<"end work"<< endl; return 0; }
datain DATASEG, and the OS does not release CODESEG. You can play with WinAPI functions, but you cannot do this on pure C / C ++. - user194374