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.

Jenia Ivanov
  • 2,485
  • 3
  • 41
  • 69
  • 3
    That sounds like you're using a C++ compiler to compile C code. Implicitly casting void* is not valid C++, but it's valid C. – rici Oct 20 '13 at 19:20

2 Answers2

2

Credits go to @rici as he answered ages ago but in order to move this to closure, the following code resolves the issue:

#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$

The issue is as explained by both the compiler and rici above: c++ does not permit assigning pointers of incompatible type. The original code would build happily with gcc (instead of g++).

maverick
  • 436
  • 2
  • 10
0

Thanks to Philip Kendall for setting me straight. :-)

It looks like you forgot to #include <stdlib.h>.

Without that, the compiler thinks that malloc() returns an int, which is obviously not the same type as timer_t *.

kmort
  • 2,848
  • 2
  • 32
  • 54
  • If this is truly C, you [shouldn't do that](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Philip Kendall Nov 20 '13 at 15:00
  • See the linked question :-) – Philip Kendall Nov 20 '13 at 15:35
  • @PhilipKendall Oh boy. I feel sheepish. Fixed the answer. Thanks for your help. – kmort Nov 20 '13 at 16:03
  • I don't think so: the error message the original poster is getting is "invalid conversion from void*", so the compiler obviously knows it's a void*. I suspect @rici is right and this is actually being compiled as C++. – Philip Kendall Nov 20 '13 at 16:17
  • @PhilipKendall In that case, one thing to note about QNX, they use uppercase `QCC` for C++ preprocessing, and lowercase `qcc` for C preprocessing. Not sure what happens after that, but somehow it figures out which libraries to use based on the input case of the call to the compiler. Anyway, if the OP is using a case-sensitive host to build this, they could easily accidentally use `QCC` instead of `qcc`. You can use `qcc -V` to specify the complier. http://www.qnx.com/developers/docs/6.3.2/neutrino/utilities/q/qcc.html – kmort Nov 20 '13 at 16:26