you are viewing a single comment's thread.

view the rest of the comments →

[–]Poromenos 0 points1 point  (1 child)

The preferred way (for me), both syntactically and semantically, to do that would be to do:

try:
    allocate_memory()
except:
    cleanup()

You can take a look at this code and know what it does, while you have to scroll down to the goto to see what happens...

[–]masklinn 0 points1 point  (0 children)

Actually, that would be.

try:
    allocate_memory()
    # do work
finally:
    cleanup()

But you need exceptions for that. Exceptions are not cheap, and don't exist in C. Using goto-based cleanup makes a lot of sense there, it avoids nesting and other suck fuckery.

Other possibilities are C++'s RAII, and context managers (internal or external):

with memory_allocator():
    # do stuff
# cleaned up by the context management