Skip to main content

CSC 282.01, Class 10: More Fun with Macros

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Questions
  • Lab!

News / Etc.

  • Sorry that the assignment come out too late. This one will be more timely.

Upcoming work

  • Homework: Something involving uthash.h or <sys/queue.h>

Good things to do

  • 11am Faculty Governance Discussions
  • 11am-1pm Spark Tank presentations
  • CS Extras at 4:15
  • James McBride at 4:15ish
  • Movie at 5 at Strand
  • Director talk at 6:30 pm JRC 101 free pizza
  • Some famous guy giving multimedia talk on Politics of Masculinity 7pm Harris
  • Music faculty performance 7:30 pm in Sebring-Lewis
  • Godspell is 7:30 pm at Loft (sold out)
  • James McBride and his band 8:00 pm in Herrick

Questions

Review

  • Macros are awesome - Text substitution is your friend
    • Common Lisp Macros are better than C macros
    • Scheme Macros may eventually be hygenic, which is even better
  • Great for logging/testing
    • Use assert
    • Write our own custom version of assert
  • Useful for writing DRY code

Detour: assert

  • It’s a macro.
  • You can see how it is defined in /usr/include/assert.h
  • Note: Consider the following
assert (sqrt(4) == 2);

This normally gets expanded to some strange code like

if (! (sqrt(4) == 2)) { fprintf (stderr, ...); exit(0); };

You can turn off assert with -DNDEBUG. If we just define assert(X) as nothing, we get

;

Two approaches

#ifdef NDEBUG
#define assert(X) (void (0))
#define assert(X) do { } while (0)
#endif

Detour: Why do we use the do ... while(0)

if (test)
  MACRO(stuff);
else
  other_stuff();

Lab

Clone https://github.com/Grinnell-CSC282/macros-2017S

Write a simple test program for stacks of int values.