News for hackers. Stuff that matters.
Re:Some dark corners of C by Eisk45 on Saturday March 09, 2013 @08:00AM (#5348275)
Could someone explain to us newbies what the #define code does?...
Re:Some dark corners of C by jfim on Saturday March 09, 2013 @08:12AM (#5348300)
It replaces the first word with what comes after. For example, #define foo bar is roughly equivalent to s/foo/bar/g, so #define else would just remove all else keywords. I'll let you guess what it does to a program.
Re:Some dark corners of C by shaneeb on Saturday March 09, 2013 @09:47AM (#5348505)
Not to forget #define struct union which will pack all struct members together. Gives me chills even to think about it...
Re:Some dark corners of C by AnthonyMouse on Sunday March 10, 2013 @12:52AM (#5350900)
Can anyone think of a reason why all the examples are keywords? I would think if you were trying to cause some trouble, redefining standard functions would cause all kinds of chaos, e.g.

  #define memmove memcpy
  #define strncmp memcmp
  #define rand fork
  #define free(x)
  #define strerror(n) strerror((n)+1)
  #define memset(addr, byte, len) memset(addr, ~(byte), len)
Re:Some dark corners of C by nostrademons on Saturday March 09, 2013 @12:46PM (#5349037)
The #define else one is scarier to me. #define struct union will almost certainly crash immediately on runtime, as one member's a pointer and it gets overwritten by an int or a pointer to a different type. #define else just always runs all else clauses - the code is likely still valid, it just does something very different from what the author intended.

Both of them would be a real pain to debug.

Re:Some dark corners of C by jongraehl on Saturday March 09, 2013 @11:50PM (#5350754)
Both would be easy to notice in a debugger. Finding the cause, as you imply, would be tricky unless you remember to check the preprocessed output.
Re:Some dark corners of C by eru on Sunday March 10, 2013 @06:47AM (#5351395)
It's a great memory saver.