Samstag, 15. März 2014

gcc's cleanup attribute instead of goto

While reading Dr. Dobbs article about Apples goto fail bug I stumbled across the gcc attribute cleanup as a potential replacement for the mighty goto.

Here is a quote from the gcc manual:

The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration.

So instead of using goto to free resources locally, at declaration of a variable you also attach a cleanup function to it. As soon as the variable goes out of scope your cleanup code is being run.

If you search the internet for "gcc cleanup" you'll find many hits. My favorite one including a nice demonstration is here.

Nevertheless, for my current projects I won't make use of this feature. Mainly because I'm happy with the simple goto mechanism (as thousands of other C developers apperently are). Also, I want to keep the C code portable.

Keine Kommentare:

Kommentar veröffentlichen