// filename:c2011-7-13-2-1-ex.c // original examples and/or notes: // (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011 // http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf // C2011 7.13.2.1 The longjmp function // compile and output mechanism: // (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, December.29, 2013 // compile errors and/or wornings: // 1 (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn) // Target: x86_64-apple-darwin11.4.2 //Thread model: posix // (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign. // 2 gcc-4.9 (GCC) 4.9.0 20131229 (experimental) // Copyright (C) 2013 Free Software Foundation, Inc. #include // Example #include jmp_buf buf; void g(int n); void h(int n); int n = 6; void f(void) { int x[n]; // valid: f is not terminated printf("f() %d\n", x[n]); setjmp(buf); g(n); } void g(int n) { int a[n]; // a may remain allocated printf("g() %d\n", a[n]); h(n); } void h(int n) { int b[n]; // b may remain allocated printf("h() %d\n", b[n]); longjmp(buf, 2); // might cause memory loss } int main(void) { f(); return printf("7.13.2.1 The longjmp function %d\n", *buf); } // output may be //f() 0 //g() 1533516432 //h() 0 //g() 1533516432 //h() 0 //gとhのくり返しが続く。