Using a define that hasn't been #defined 
Author Message
 Using a define that hasn't been #defined

Recently I've run into a couple of hard-to-track bugs because I have
a section of conditionally compiled code using a symbol which hasn't
been defined.  For example, take the trivial case of this "program"

-------

#define XYZZY 1

main() {
#if XYZZU
        printf("xyzzy\n");
#else
        printf("plugh\n");
#endif

Quote:
}

------

Now, obviously that XYZZU was supposed to be an XYZZY, and I wanted to
conditionally compile the first piece of code, not the second.  But when
I compile this, it doesn't even give me a friendly warning, and just
assumes XYZZU is 0.  (Which, I understand, is in accordance with ANSI-
defined behavior.)

Now, my question is how other people avoid these same kinds of idiotic
mistakes?  Even if I put in "#if !defined(XYZZY)..." type things for
every define, it still wouldn't catch all typos.  There should be some
way to avoid this idiotic kind of error...
--
Jim Seidman (Drax), the accidental engineer.
"There's a certain freedom to being completely screwed." - The Freshman
UUCP: ames!vsi1!headland!jls



Sun, 21 Mar 1993 07:26:43 GMT  
 Using a define that hasn't been #defined

Quote:

>#define XYZZY 1
>...
>#if XYZZU

I've always wished that the preprocessor had used modern language-design
principles and made undefined variables illegal rather than synonyms for
zero.  Unfortunately there was a large body of code that depended on the
sloppiness, so now we're stuck with it.

The best we can hope for is to get compiler vendors to provide an option to
warn whenever an undefined symbol is used in a preprocessor conditional
(other than as the operand of defined(), of course).  Then the next step
is to get software vendors to use "#if _S_UNIX" instead of "#ifdef _S_UNIX"
to test the compilation environment--that way, it becomes possible to
distinguish "symbol not set because this isn't UNIX" from "symbol not set
because that's not the name I use to flag what you're trying to test".




Sun, 21 Mar 1993 12:09:21 GMT  
 Using a define that hasn't been #defined

        In fact, there is a very good way to avoid this kind of error: use the
compiler directives #ifdef and #ifndef.  These check to see if a given
identifier has or has not been #define'd.

        Example:

        #ifdef MSDOS
        do_dos_specific_stuff();
        ...
        ...

        Hope it helps.

---
John Gordon


GEnie:    j.gordon14                  



Sun, 21 Mar 1993 10:21:32 GMT  
 Using a define that hasn't been #defined

Quote:

>Recently I've run into a couple of hard-to-track bugs because I have
>a section of conditionally compiled code using a symbol which hasn't
>been defined...
>Now, my question is how other people avoid these same kinds of idiotic
>mistakes?  Even if I put in "#if !defined(XYZZY)..." type things for
>every define, it still wouldn't catch all typos.  There should be some
>way to avoid this idiotic kind of error...

Two rules that can help with this and related problems are:

1. When enumerating values for a code, start at 1, not 0.

2. Make sure that if the user hasn't specified a choice (e.g. hasn't
        defined one of several symbols tested with #ifdef, or hasn't
        given a non-zero value to something tested with #if) that
        the result is a complaint, not a default choice.
--
Imagine life with OS/360 the standard  | Henry Spencer at U of Toronto Zoology



Mon, 22 Mar 1993 02:03:00 GMT  
 Using a define that hasn't been #defined

Quote:

>    In fact, there is a very good way to avoid this kind of error: use the
>compiler directives #ifdef and #ifndef.  These check to see if a given
>identifier has or has not been #define'd.

I'm not the one who asked about this in the first place, but I really
don't see how that would be expected to alleviate the situation.  You
see, I recently encountered a bug of a similar character (heh), as
follows:

#define SYSV

{
    ...
#ifdef SYSY
    ...
#endif
    ...

Quote:
}

In this situation, whether you use #if or #ifdef doesn't make any
difference.  To exasperate the situation, I was using a terminal
upon which capital 'V's and 'Y's look even more similar than normal,
and the problem resulting from the #ifdef-ed code's absence only
manifested itself much later, in another module entirely separate
from the one where the bug was hiding.

That one took days, and large chunks of my hair, to find.  B-(

I suppose that, if one is very concerned about this and is using
an ANSI-compliant preprocessor, one could do something like this:

#include <stdio.h>

#define SYSV    1
#define DOS     2
#define OSTYPE  SYSV

main()
{
#if OSTYPE == SYSV
        printf( "This is the System V-specific code.\n" );
#elif OSTYPE == DOS
        printf( "This is the System V-specific code.\n" );
#else
#error OSTYPE undefined or mistyped
#endif

Quote:
}

--
 \\         David Sandberg          \     ,=,       ,=,           \\

 \\  uunet!rosevax!sialis!quad!dts  \     '=\       `='           \\


Sun, 21 Mar 1993 16:47:07 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. #define defining other #defines

2. defining function whose type same as its arguments and used as another's argument

3. #define - cannot define/undefine preprocessor symbols after first token in file

4. #defines with #defines inside them?

5. #define with several #defines inside???

6. Preprocessor: defining a macro inside a #define directive

7. #define a define

8. #define REM /* #define ARK */

9. Legal to #define #define?

10. #define void int vs. #define void char

11. get #define as result of #define ?

12. Possible to use #define to define global variable?

 

 
Powered by phpBB® Forum Software