Hacker News new | past | comments | ask | show | jobs | submit login

My fav is one that I've actually had to use in practice when using an (arcane) platform specific builtin. It's a way to arbitrarily align variables:

   #define ALIGN(type, name, align) __ALIGN_MASK(type, name, (align - 1))
   #define __ALIGN_MASK(type, name, mask) char __##name##_buffer[sizeof(type) + mask];\
   int * name = (type *)((size_t)(__##name##_buffer + mask) & ~mask);
Then you can just use it in the code like so:

   ALIGN(int, y, 16);
And you've just declared a pointer to a 16-byte aligned int on the stack. Very useful.



That's all very well, except that it's wrong. The last line needs to do "(buffer + mask) & ~mask" or you'll end up pointing outside the array.

You also violated the C standard in a less severe way. According to the standard, any identifier name starting with a double underscore (or underscore followed by upper-case letter) is reserved by the implementation for any use. These reserved names are frequently used in system header files, and encroaching on that namespace can easily lead to weird errors if the code is ever compiled on some other system.


Whoops, that's what I get for prettying things up after copy and pasting. Thanks for pointing that out.

As for the double underscore being reserved for implementation use, this was in fact part of the compiler (well, runtime in this case) implementation, again a side-effect of copy and paste.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: