3

I've got an interesting problem that I am sure someone would have come across. I am writing the front end UI in Objective C Cocoa, and the backend in C++. In C++ I have

#define NULL 0

Unfortunately, this has dire consequences for nil. Especially with nil terminated function calls as I now get this warning - "Missing sentinel in method dispatch", which I assume means it couldn't find the nil terminator. This is the only definition I could find for nil:

#ifndef NULL
#define NULL    __DARWIN_NULL
#endif /* ! NULL */
#ifndef nil
    #define nil NULL
#endif /* ! nil */

which seems to me that nil is NULL, and that my earlier define for NULL is messing everything up although I don't know how. The NULL is defined in C++ so that it can be platform independent. I have tried redefining NULL and nil, but nothing seems to take. Any suggestions on the correct way to go about this would be appreciated.

AndyTang
  • 547
  • 7
  • 24
  • I think I understand what you want to do... but could show a specific example? – nacho4d Sep 01 '11 at 13:36
  • Basically 'nil' stops working correctly once I used #define NULL 0. So things like [[NSGradient alloc] initWithColorsAndLocations:... nil] would not work with that define. But the define is needed in the backend. – AndyTang Sep 01 '11 at 14:35
  • Why do you even need that definition? C++ code can just use `0`; the `NULL` macro is there only for backwards compatibility with C. – MSalters Sep 01 '11 at 15:10

4 Answers4

4

In either C or C++, attempting to define NULL yourself leads to undefined behavior. Sorry, but you're just allowed to do that. Instead of trying to define it yourself, you need to include one of the headers that already defines it for you.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I think it's quite specific to this case because C++ code can co-exist with Objective C code. For example, if I decided to use a Java front end, or a Javascript frontend, NULL would not be defined. – AndyTang Sep 01 '11 at 14:38
1

Reminded me of this question I saw recently: How to wrap a C++ lib in objective-C?

So, what about:

#ifdef __cplusplus
#define NULL 0
#endif 
Community
  • 1
  • 1
w-m
  • 10,772
  • 1
  • 42
  • 49
0

You could #define it while it is being used within your .h/.cpp. Then, at the end of those files, #undef NULL so that the ObjC's definitions take over. That way the definition is cleaned-up.

AWF4vk
  • 5,810
  • 3
  • 37
  • 70
0

You can also simply use 0 in C++ instead of NULL.

Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38
  • Then you as the human programmer run into the same problem the Objective-C compiler is having: Did you mean to pass a zero there, or the null pointer constant? Goes triple in function and C++ method calls, where the arguments aren't named in the call, so you can't always tell whether something expects a number or a pointer. `NULL` and `nil` exist for this reason. – Peter Hosey Sep 01 '11 at 17:29
  • Well, `NULL` is just a `#define`, so it doesn't really serve any purpose (except making the code a bit clearer, for some people). Personally, I prefer to use 0, just as Stroustrup (IIRC), but that's more a question of style than of substance. C++0x offers `nullptr`, which is a better alternative (it is type safe, i.e. you cannot use it as argument of a function that expects an `int`, and it's as clear as `NULL`). – Andrea Bergia Sep 01 '11 at 22:27