1

When defining a callback proc in Xt (for example XtTimerCallbackProc), client_data is specified as an XtPointer. Is it safe to pass an int via client_data, rather than an actual pointer, and cast it back inside the procedure?

For example:

void foo(void) {
   ...
   int data = 1;
   XtAppAddTimeout(app_context, 1000, timer_cb, data);
   ...
}

void timer_cb(XtPointer client_data, XtIntervalId *timer)
{
   int my_data = (int) client_data;
   ...
}
Nikola Smiljanić
  • 26,745
  • 6
  • 48
  • 60
ToddR
  • 300
  • 1
  • 11

2 Answers2

2

Yes; the API uses a pointer because the size of this type is >= the size of an int on almost any computer/compiler combination, so you can use it to pass most things except double simply by casting.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

It should be safe, but you should use an integer type that have the same size as a pointer.

The standard C99 type uintptr_t can be used for this purpose.

As per comments point out, int->ptr->int is not guaranteed by the use of this type. It should work more than often, but it is not as per specifications.

The alternative is to pass a pointer. If your app is single threaded, you might just do

static int data; and passs &data. If your app needs to be reentrant, then malloc an int.

As a matter of fact, I don't think the standard support int->ptr->int in any way.

  • Found in `#include `, in case OP doesn't know about it. – ephemient Dec 11 '09 at 18:28
  • From what I have read, uintptr_t is guaranteed to work for ptr->int->ptr, but not necessarily int->ptr->int. So the short answer appears to be no, there is no portable way. Is that correct? – ToddR Dec 12 '09 at 16:15