#include #include struct save_rec { void *p; /* where */ void *v; /* saved value */ size_t sz; /* size of value */ }; static void restore (struct save_rec *s) { memcpy (s->p, s->v, s->sz); } #define dynbind(lvalue,val) \ for (struct { \ int k; \ __typeof__(lvalue) old; \ } rec##__LINE__ = { 0, (lvalue) }; \ rec##__LINE__.k < 1;) \ for (struct save_rec save##__LINE__ \ __attribute__ ((cleanup (restore))) \ = { &(lvalue), &rec##__LINE__.old, sizeof (lvalue) }; \ rec##__LINE__.k < 1;) \ for ((lvalue) = (val);rec##__LINE__.k < 1;rec##__LINE__.k++) /* Demo */ int xyz = 0; struct point { int x, y; } pt; extern int bar (void); int foo (void) { dynbind (xyz, 42) { return bar (); } } int foo_pt (void) { struct point q = { 100, 200 }; dynbind (pt, q) { return bar (); } }