Using a define that hasn't been #defined

T

Tony Burden

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

}

------

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...
 
I

Ian Collins

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

}

------

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...

Unit tests.
 
B

Ben Pfaff

Tony Burden said:
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...

If you write your code carefully, so as never to use a macro that
is not defined, then you could turn on a compiler warning about
an undefined macro in an #if directive. GCC has -Wundef, for
example.
 
T

Thad Smith

#define XYZZY 1

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

}

------
Now, my question is how other people avoid these same kinds of idiotic
mistakes?

main() {
#if XYZZU == 1
printf("xyzzy\n");
#elif XYZZU == 2
printf("plugh\n");
/* etc */
#else
#error no valid definition for XYZZU
#endif

}
 
J

Jorgen Grahn

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

}

------

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.)

Yes, I have been bitten by that one too.
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...

(a) Macros suck, so don't expect it to be painless or safe. Google
"ifdef considered harmful" by Henry Spencer et al.

(b) If you can keep it a "boolean", use #ifdef FOO and #define FOO, and
try to stay away from #if and #define FOO 1.

/Jorgen
 
T

Tom St Denis

main() {
#if   XYZZU == 1
          printf("xyzzy\n");
#elif XYZZU == 2
          printf("plugh\n");
/* etc */
#else
        #error no valid definition for XYZZU
#endif


DING DING DING DING, winner.

Though if it's a binary flag I'd just use "ifdef" or "if defined(...)"
since it's less error prone.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top