"Martijn" <subscription_REMOVE_101@hot_REMOVE_mail.comwrites :
I hope I am in the right place on preprocessor help, 'cause the GNU
preprocessor documentation is somewhat minimal on this topic.
Assume I have two #define'd constants:
#define STRING1 "string"
#define STRING2 NULL
The code I want to compile is something like this:
char *sz1 = NULL;
char *sz2 = NULL;
if ( STRING1 )
sz1 = strdup(STRING1);
if ( STRING2 )
sz2 = strdup(STRING2);
(traded style for brevity)
I tried a conditional micro at first ( #if STRING1 ), but that makes the
preprocessor choke.
Right, a preprocessor #if condition knows nothing about null pointers
or string literals.
If I use the construct above, I get a warning for each
time I use it (about strdup requiring a non-NULL argument).
First, I'll mention that strdup() is not a standard C function.
A compiler is allowed to warn about anything it likes. Apparently
yours is smart enough to issue a warning for strdup(NULL), but not
smart enough to recognize that the call will never be executed because
you've already checked the value. (BTW, gcc doesn't give me that
warning; maybe you're using different options.)
It's conceivable that the warning will go away if you increase the
optimization level, causing the compiler to do more analysis, but
that's not a good solution. Ideally, you should increase the
optimization level if you want more optimization, not to get rid of
warnings.
You can probably restructure your code to eliminate the warning, but
the cure could easily be worse than the disease. There might be an
option to disable that warning, but it's a useful warning in some
cases.
A good rule of thumb is to eliminate all warnings, but it's only a
rule of thumb. There *are* times when you know better than the
compiler; this may be one of them.
Or there may be a cleaner way to write this code that also avoids the
warning, but it's hard to tell without seeing more of your code.
--
Keith Thompson (The_Other_Keith)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.