6 ms·
in a pure C, without operator overloading and threads: int aa = 0; #define a (++aa) if(a == 1 && a == 2 && a == 3) { printf("pff"); }
by mkup 9y ago
in a pure C, without operator overloading and threads:
int aa = 0;
#define a (++aa)
if(a == 1 && a == 2 && a == 3) {
printf("pff");
}
- anfractuosity 9y agoHeh nice, that's clever.
- justinjlynn 9y agoit's essentially an explicitly clocked data race. evil.
- Sharlin 9y agoHeh, my knee-jerk reaction was to complain that that's undefined behavior but for once it's not! The logical operators && and || are defined to establish sequence points due to their short-circuiting evaluation.
- adrianratnapala 9y agoWell done. Whileone forgets evil macros are, we often forget how versatile they are in that cause. That said, I feel that in practice I have to worry more about this strange hidden behaviour in C++ and Python than in C and JavaScript. And even Python is not so bad if you stick to the standard libraries.
- chias 9y agoUntil you start playing with ctypes, where you can change the values of constants. In Python 3: >>> import ctypes >>> ctypes.memmove(id(5)+24, id(23)+24, 8) >>> 2 2 >>> 3 3 >>> 2 + 3 23 Or, similarly for Python 2 >>> import ctypes >>> ctypes.cast(id(4),ctypes.POINTER(ctypes.c_int))[4]=5 >>> 2 2 >>> 2 + 2 5
- zeveb 9y agoHere's a Common Lisp version: (let ((b 0)) (symbol-macrolet ((a (incf b))) (and (= a 1) (= a 2) (= a 3)))) For those not familiar with them, symbol macros let a symbol be expanded to an arbitrary expression, kinda like a clean version of the C preprocessor.