0

In this function: omapi_wait_for_completion

omapi_object_t *inner;

if (object) {
       waiter = (omapi_waiter_object_t *)0;
       ....

0 is being cated to an omapi_waiter_object_t pointer. What is the purpose?

I know a similar question exists: Casting NULL to a struct pointer in C?, but the answer is about validity using macro. In my case, no macro involves. I want to know why it is cast that way.

Community
  • 1
  • 1
Amumu
  • 17,924
  • 31
  • 84
  • 131
  • I guess the cast is there to prevent C++ compilers to complain when fed the source code in question. Why you would want to stop the compiler from complaining when it doesn't understand something is another question. – pmg Apr 22 '14 at 10:08
  • @pmg (paraphrased you): In C++, 0 is a perfectly valid null pointer constant. – Deduplicator Apr 22 '14 at 10:09
  • Oh! I don't know C++. Is `0` in a pointer context taken as `void *`? I think in C++ you can't mix and match `void` pointers as wildly as in C. – pmg Apr 22 '14 at 10:10
  • A literal 0 is a nullpointer constant in a pointer context, just like in C. `(void*)0` is only one in C, due to C++ decision to cripple `void*`. – Deduplicator Apr 22 '14 at 10:12
  • This is C, not C++. The code is from ISC's DHCP server. – Amumu Apr 22 '14 at 10:19

1 Answers1

3

Sometimes, people just write useless code.

There is no sense in actually writing the cast here.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    Well, it is the answer, even if that's all there is to it. – Deduplicator Apr 22 '14 at 10:10
  • I see. Probably the writer just wants the type of the pointer to be available when someone reads his code, although in this example the declaration is a few lines above. There maybe other places where pointer casting and pointer declaration are screens apart, or in other files, and having the type casting right there can be useful for code reading. After all, there's no harm in writing like that. – Amumu Apr 22 '14 at 10:31
  • @Amumu: As far as it goes, that would be a very flimsy excuse, which I wouldn't buy. Especially for casts, as they are telling the compiler "trust me and do what I say". – Deduplicator Apr 22 '14 at 10:35
  • mostly people post useless answers too! this Null constant pattern is the heart Linux kernel linked list implementation. I'm sorry to say but cant buy this answer. – Hrishikesh Jan 16 '21 at 20:35
  • @Hrishikesh Is it a pattern? Too little substance imho. Did it find its way into some repository? Still wouldn't make it better. Anyway, [I cannot find it in the Linux linked-list implementation](https://github.com/torvalds/linux/blob/master/include/linux/list.h) – Deduplicator Jan 16 '21 at 21:04
  • @Deduplicator there you go, my friend, https://github.com/torvalds/linux/blob/fcadab740480e0e0e9fa9bd272acd409884d431a/tools/include/linux/kernel.h#L23. My bad I could have posted the complete answer. – Hrishikesh Jan 17 '21 at 22:48
  • @Hrishikesh You mean the `offsetof`-macro? That's something completely different though, despite containing a cast null pointer literal. – Deduplicator Jan 17 '21 at 23:12