On 2026-10-20, Lawrence DrCOOliveiro <ldo@nz.invalid> wrote:
On Tue, 20 Oct 2026 19:04:55 GMT, Charlie Gibbs wrote:
The undisciplined use of GOTOs is bad, agreed.
Is there such a thing as a *disciplined* use of gotos?
Yes, especially in COBOL or assembly language. For instance,
a GOTO to the end of a function ensures any of its cleanup code
gets executed, and fulfils a goal of structured proramming:
each function has one exit point.
I find the C code I write tends to do a lot of dynamic resource
allocations. These can sometimes fail with errors. So error recovery
will invariably involve cleaning up those parts of the allocations
which already succeeded. Gotos are no good for this; you need to exit
levels of structure nesting one by one, disposing of allocations as
you go.
I know that a lot of CS types are going to stick pins into little
dolls that look like me for saying this, but IMHO resource pointers,
handles, etc. should be global. Possible exceptions are resources
that are going to be used solely by the current function - but even
then, if that function calls another function that fails horribly,
there's stuff that might not get cleaned up. That's why all my
C programs contain a function quit_cleanup(char *errmsg), which
de-allocates all resources and then exits (displaying "errmsg"
if it isn't NULL). I can call that function anywhere, safe in
the knowledge that by the time it finishes (it never returns),
everything will have be freed.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 76 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 55:54:39 |
| Calls: | 1,265 |
| Calls today: | 1 |
| Files: | 1,362 |
| D/L today: |
3 files (2,684K bytes) |
| Messages: | 285,712 |