【发布时间】:2014-10-16 16:52:29
【问题描述】:
我在 Solaris 5.10 64 位上使用带有标志的 CC 5.10 编译了以下 C 代码 -m64 -KPIC -x04
header.h
typedef struct functions
{
double (* pfComputeGeneric) (myStruct *, myStruct *, double, double *, int);
} functions;
...
double myCompute(myStruct *, myStruct *, double, double *, int);
source.c
double myCompute(myStruct * px1, myStruct *px2, double d1, double *pd1, int i1)
{
// Do stuff with px1
}
...
myStruct *pxStruct = alloc(...);
functions *pxFunctions = alloc(...);
pxFunctions->pfComputeGeneric = myCompute;
...
double dResult += pxFunctions->pfComputeGeneric(pxStruct, pxStruct, 0.0, NULL, 0);
source.c 中的代码运行良好(没有什么奇怪的),直到我通过函数指针 pfCompute 进入 myCompute,其中 px1 损坏。我不知道为什么。
将通过 pfCompute 的调用替换为直接调用 myCompute 可以解决问题。
删除 -x04 选项也可以解决问题。
我查看了this question 的答案,但我确定我没有弄乱指针大小。
【问题讨论】:
-
} functions后面少了一个分号。 -
也许分配有误,或者在您省略的部分中发生了一些狡猾的事情。请编辑您的问题以包括分配,或者更好:发布MCVE。
-
@wildplasser :确实,谢谢。
-
也许函数没有期望接收到 px1 和 px2 都指向同一个内存?
-
@NisseEngström 这是我的第一个猜测:我检查了 Windows 32 位、Linux 32 位和 64 位以及 Solaris 32 位。代码在所有这些平台上运行良好。净化/博士。记忆也不抱怨。我怀疑那里的选项 (-x04) 有副作用(错误?)。我不能真正包含完整的代码(这是一个高度简化的代码版本,有几百行代码)。实际上,发布这个问题,我想知道在具有 -x04 的 Solaris 64 位上使用函数指针是否存在一些已知问题。
标签: c function-pointers solaris