2

My code is something like this:

#define ZERO_CONSTANT 0
foo(size_t * p_size);

foo(ZERO_CONSTANT); //Doesn't warn

Which gcc flag will make the call foo(ZERO_CONSTANT) to warn?

Felipe Lavratti
  • 2,887
  • 16
  • 34
  • 3
    Unfortunately you're allowed to pass `0` for `NULL` so I don't think there is much you can do about this. – Paul R Sep 22 '14 at 15:11
  • As Paul says [0 is a pointer literal](http://stackoverflow.com/q/24212792/1708801). – Shafik Yaghmour Sep 22 '14 at 15:16
  • My first question would be, why do you think this should be warned about? What is the error that you would be trying to catch? Do you want to be warned when your function is called with any kind of `0`? – Jens Gustedt Sep 22 '14 at 15:26
  • @JensGustedt The point is semantics, not syntaxes. ZERO_CONSTANT has a meaning that has nothing to do with pointers or addresses. – Felipe Lavratti Sep 23 '14 at 17:16

3 Answers3

5

No compilation flag can possibly help you here. The C standard defines the literal 0 to stand in for the null pointer value of any type. Quite often you'll see 0, NULL and (void*)0.

(Note well that this does not imply that the memory address is 0. Because it's undefined behaviour in general to assign an integral value to any pointer type, 0 is used as a placeholder literal for the null pointer).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • The "could not possibly" bit is not true. Compilers are perfectly free to warn about anything, including 100% legal standard conforming things (and often do). – n. m. could be an AI Sep 22 '14 at 15:29
  • @n.m. True but I think I'll retain my assertion. I'm thinking of allowing that compilation flag in my "undefined behaviour nasal demon" compiler I'm working one. – Bathsheba Sep 22 '14 at 15:33
3

There is a new option (available since [probably] GCC 4.7.x) - -Wzero-as-null-pointer-constant. It does what you expect, but is valid only for C++... In C the code you presented is perfectly legal, as NULL is usually just "0".

Freddie Chopin
  • 8,440
  • 2
  • 28
  • 58
  • Many implementations of the `stddef.h` header define `NULL` as `(void*)0`, the C standard (ISO 9899:2011) allows both `(void*)0` and `0`. – fuz Sep 22 '14 at 17:01
1

As has been mentioned, it's perfectly valid to pass 0 as a pointer. You can generate a warning with some compilers though if you are prepared to change the definition for ZERO_CONSTANT:

#define ZERO_CONSTANT ((int)0)

This then generates a warning with clang:

warning: expression which evaluates to zero treated as a null pointer constant of type 'size_t *' (aka 'unsigned long *') [-Wnon-literal-null-conversion]
expanded from macro 'ZERO_CONSTANT'
#define ZERO_CONSTANT ((int)0)

Unfortunately it looks like gcc (4.8) still does not generate a warning in this case, even with -Wextra, although it's possible that there is a further -W switch that might enable this.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • Couldn't enable -Wnon-literal-null-conversion flag in gcc nor find anything on google about it. Can you provide reference? – Felipe Lavratti Sep 22 '14 at 15:32
  • Sorry - that's a clang warning - I haven't checked gcc and just assumed that it would have something similar. I'll update the answer. – Paul R Sep 22 '14 at 16:15