0

I have written a program that i compiled and ran under Linux. It worked correctly. I was then forced to port it to QNX.

I did and when i tried to compile it there (qnx with momentics) I got a cryptic error:

timer_t * timer = malloc(sizeof(timer_t)); <---- invalid conversion from 'void*' to 'timer_t*'

here i get another error of a (i guess) similar type:

static void signalor(int sig, siginfo_t *si, void *uc)
    timer_t *tidptr;
    tidptr = si->si_value.sival_ptr;<----- invalid conversion from 'void*' to 'timer_t*'

Does anyone know why i get this error? Or how to fix it?

thanks in advance.

4

2 回答 2

2

正如他很久以前回答的那样,归功于@rici,但为了将其移至关闭,以下代码解决了该问题:

#include <malloc.h>
#include <time.h>
#include <signal.h>

int main() {
    timer_t * timer = (timer_t*)malloc(sizeof(timer_t));

    siginfo_t si = {0};
    timer_t *tidptr;

    tidptr = (timer_t*)si.si_value.sival_ptr;

    return 0;
}

bash-3.2$ ntoarmv7-g++ tst.c -Wall
tst.c: In function 'int main()':
tst.c:7: warning: unused variable 'timer'
bash-3.2$

上面的编译器和 rici 都解释了这个问题:c++ 不允许分配不兼容类型的指针。原始代码可以使用 gcc(而不是 g++)愉快地构建。

于 2013-12-03T07:06:39.233 回答
0

感谢菲利普肯德尔让我直截了当。:-)

看起来你忘了#include <stdlib.h>

没有它,编译器会认为它malloc()返回一个 int,这显然与timer_t *.

于 2013-11-20T14:59:10.737 回答