有人可以向我解释为什么作者会像这样初始化指向内存位置的 void 指针吗? 我只是一个初学者,我以前从未见过这样的符号。
void executeCode(){
char* MEMORY_BUFFER = (char*)VirtualAlloc(NULL, sizeof(someCode), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
memcpy(MEMORY_BUFFER, someCode, sizeof(someCode));
(*(void(*)())MEMORY_BUFFER)();
}
最佳答案
char *MEMORY_BUFFER = /* whatever */;
MEMORY_BUFFER
是一个指向 char 的指针。您不能“执行字符串”。
如果有指向函数的指针,则可以执行该函数。
int (*fxptr)(void) = rand;
printf("%d\n", fxptr()); // execute rand() through the function pointer
printf("%d\n", (*fxptr)()); // dereferencing the function pointer is redundant
Actor 阵容
(void(*)())MEMORY_BUFFER
“转换”(如果有效)MEMORY_BUFFER
到一个指向函数的指针,该函数接受未指定数量的参数并且不返回任何内容。
您可以通过该指针调用该函数(假设它是有效的)
((void(*)())MEMORY_BUFFER)();
(*(void(*)())MEMORY_BUFFER)(); // no need to dereference function pointer
关于c++ - C中奇怪的指针表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60476408/