如何将varargs与win64上的C中的函数指针结合使用?
-
26-10-2019 - |
题
考虑以下C程序:
#include <stdio.h>
#include <stdarg.h>
typedef void (callptr)();
static void fixed(void *something, double val)
{
printf("%f\n", val);
}
static void dynamic(void *something, ...)
{
va_list args;
va_start(args, something);
double arg = va_arg(args, double);
printf("%f\n", arg);
}
int main()
{
double x = 1337.1337;
callptr *dynamic_func = (callptr *) &dynamic;
dynamic_func(NULL, x);
callptr *fixed_func = (callptr *) &fixed;
fixed_func(NULL, x);
printf("%f\n", x);
}
基本上,这个想法是在“通用”函数指针中存储一个带有可变参数的函数。作为比较,我还将另一个功能与固定参数列表一起。现在看看在X86 Linux,AMD64 Linux,Win32和Win64上运行此操作时会发生什么:
$ gcc -m32 -o test test.c
$ file test
test: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ./test
1337.133700
1337.133700
1337.133700
$ gcc -o test test.c
$ file test
test: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.9, not stripped
$ ./test
1337.133700
1337.133700
1337.133700
C:\>gcc -o test.exe test.c
C:\>file test.exe
test.exe: PE32 executable for MS Windows (console) Intel 80386 32-bit
C:\>test.exe
1337.133700
1337.133700
1337.133700
C:\>x86_64-w64-mingw32-gcc -o test.exe test.c
C:\>file test.exe
test.exe: PE32+ executable for MS Windows (console) Mono/.Net assembly
C:\>test.exe
0.000000
1337.133700
1337.133700
为什么动态函数从WIN64上的变量参数列表中获得零值,而在其他任何配置上都没有获得零值?这样的事情甚至合法吗?我认为这是因为编译器没有抱怨。
解决方案
您的代码无效。调用variadic函数需要一个原型,表明它是variadic的,并且您使用的功能指针类型不能提供此功能。为了使呼叫不调用不确定的行为,您必须施放 dynamic_func
像这样的指针来打电话:
((void (*)(void *, ...))dynamic_func)(NULL, x);
其他提示
即使不需要,您也应该处理一致的功能定义,即使这意味着使用varargs。最好的是根据需要保持详细信息。
...
typedef void myfunc_t(void *, ...);
...
myfunc_t dynamic;
void dynamic(void * something, ...)
{
...
}
...
int main()
{
double x = 1337.1337;
myfunc_t *callnow;
callnow = &dynamic;
callnow(NULL, x);
printf("%f\n", x);
}
不隶属于 StackOverflow