Minor irritation with void pointers
Ju-Han Soon

using Dev C++ here... coding in C.

I'm using install_param_int here, and it accepts a void pointer as an arguement. If I send an int pointer as the arguement, it works but I've got install_param_int in quite a few places. This creates a hell alot of Warning signals from the compiler and it floods the compiler error screen so much that I find it hard to tell my real problems sometimes.

So i just created a void *pointer, pointed it at the address of the int, and used that as the arguement. Now I got a different warning "assignment discards qualifiers from pointer target type".

Can anyone tell me how to disable specific warnings for Dev C++ (or MingW... i duno which is the culprit)? Its really bugging me.

Thanks
=Han=

Steve Terry

Typecasting your int to a void pointer should work. (void*)&my_int? but that doesn't seem right.

Fladimir da Gorf

Compile the project in C, not C++.

Simon Parzer
Steve Terry said:

Typecasting your int to a void pointer should work. (void*)&my_int? but that doesn't seem right.

Why doesn't it seem right? It's the only way if you're using C++!

BAF
int myint = 42;
install_param_int(..., (void *)&myint);
...
void mything(void *param)
{
   int theint = (int *)param;
//   theint should == 42
}

Ju-Han Soon

Thanks! I just needed to know about (void*)&my_int thats all... and it worked! no more warnings.

=Han=

Kitty Cat

Assuming myint won't leave scope while the timer is running (eg. a local variable). If it does, your program will crash when it tries to access invalid memory. For example:

int main()
{
  int mynum;
  allegro_init();
  install_timer();
  intall_param_int(myfunc, 10, (void*)&mynum);
  install_keyboard();
  install_mouse();
  set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);

  rest(1000);
  return 0;
}

Here, main will return and invalidate mynum, which the timer can still try to use before Allegro's exit handlers properly stop and close the timer functions. As well, changing mynum in main will change the value in the timer (and vice versa).

Thread #556700. Printed from Allegro.cc