TITLE: Example of resource acquisition is initialization [ This article popped out on the tail end of some discussions on the impact of using gotos in code. -adc ] RESPONSE: metzenerd@aol.com (MetzenerD) [ ... ] As to avoiding GOTO's. I saw a sample a little while back that used flags to determine of cleanup was needed for each of the functions that require it. At the end of the routine, if cleanup was required, it was done. No GOTO's were used and the code was still readable without nested if's. As I have stated many times before. I have yet to see a REAL need for GOTO's in any language C, C++, BASIC (newer implementation), Pascal, etc... RESPONSE: kanze@us-es.sel.de (James Kanze US/ESC 60/3/164 #71425), 31 Aug 94 Isn't the real C++ idiom is to use scope and destructors to ensure clean-up, and not use flags or goto. I might add that I already used scope in C++, and found it very efficient. Destructors are needed, though, in the presence of exceptions (i.e.: an elaborate goto). I believe that if you design your algorithms so that you enter each block from the top, leave it from the bottom, and only declare things in the block which actually needs them, you will find that you can do very well without gotos *and* without extra control variables to manage cleanup. You will probably also find that the code is considerably more maintainable, since local modifications tend to stay local (i.e.: you don't have to modify clean up code at the end of the function). C++ goes one step further: by declaring local classes, you can write the cleanup code directly adjacent to where you do whatever you need to clean up. For example: void f() { // Lots of code here... // Now I need memory from the heap: class XPtr { public : XPtr() : ptr( new char[ 10 ] ) {} ~XPtr() { delete [] ptr ; } operator char*() { return ptr ; } private : char* ptr ; } p ; strcpy( p , "init" ) ; // For example. // And any more code that you like } Actually, the specific case of allocating/freeing memory is so frequent, I would probably just use Fergus Henderson's HeapPtr template, which he posted here a short time ago. But the above solution is total general.