0

There is a function which takes a function pointer argument of the form void *(*func)(void *) and I am reading some code which calls it with the following argument (void *(*)(void *))(-1).

Is this just casting -1 to the correct function pointer type or is it something else? I am unsure of what it means.

8
  • 1
    Hint: Always try to replace such constructs with a typedef. Things will look much simpler then. Commented Aug 5, 2012 at 4:24
  • I suspect this is a magic constant in some weird pthreads implementation. No function other than pthread_create() takes a function pointer both accepting and returning a void pointer. Commented Aug 5, 2012 at 5:05
  • @CarlNorum does memcpy take a function pointer? I believe no (unless you're trying to copy code when writing an exploit). Commented Aug 5, 2012 at 5:08
  • @H2CO3 Yes this is from the GNU Pth source. Commented Aug 5, 2012 at 6:54
  • @Michael no. Memcpy itself takes and returns a void *, but it doesn't take a function returning and taking void *. If you were right, memcpy would be declared as memcpy(void *(*)(void *)); which it isn't. Commented Aug 5, 2012 at 7:00

2 Answers 2

2

Yes, that's just a typecast. Without seeing the code in context, it's not really possible to decide why someone might be doing that, though. It's often convenient to use cdecl to answer questions like this one:

$ cdecl Type `help' or `?' for help cdecl> explain (void *(*)(void *)) cast unknown_name into pointer to function (pointer to void) returning pointer to void 
Sign up to request clarification or add additional context in comments.

1 Comment

cdecl.org also has a nice web interface to cdecl(1) (example)
0

Yes. It is trying to cast -1 to the function pointer type which should generally not be done (unless there is some really valid reason and logic there).

5 Comments

It's not necessarily wrong—it could just be a sentinel value, and as long as the code doesn't dereference it it'll be fine—but it's strongly indicative of a poor design.
@Jay it may not be wrong! See my comment on the question about pthreads.
@Adam & H2CO3, Ok. Got it. I will word it differently. But, even then, I feel for pointers NULL can be used instead of -1. Would be great if you could tell me if using -1 will be better than NULL in some case? Thanks.
@Adam & H2CO3, edited my answer. But would surely appreciate if one of you can help me understand, why the pthread code would do this. That is for my own understanding and learning purpose. Thanks Again.
@Jay: Yes, NULL could certainly be used instead. But it's possible that a function might have the behavior such that a parameter of NULL means "default behavior", a parameter of (function pointer)-1 means "other default behavior", and any other parameter means "call this function". See, for example, the definitions of the macros SIG_IGN and SIG_DFL in <signal.h>, which are used in place of a function pointer in the call to signal(2).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.