3 ms·
> Also code where you have to undo all of the work already done in the function up until that point, which can get a little tedious, i.e.:- Sorry pal, you're d
by asveikau 13y ago
> Also code where you have to undo all of the work already done in the function up until that point, which can get a little tedious, i.e.:-
Sorry pal, you're doing it wrong. At least, this is not the way I've done it and seen it done in large C code bases.
You're supposed to have only one return statement, and one block that frees everything. For example if you initialize `foo`, `bar`, and `qux` to `NULL`. Then testing these pointers for `NULL` de facto tells you how far you got in the function, and which buffers need to be freed. Just before your one single return statement (can't emphasize this enough) you call `free` on all of them regardless of success or failure. It's much more composeable than what you described - allocations for `foo`, `bar`, `qux` can fail, the ones that will be not yet allocated at that point in time will be `NULL`, and `free(NULL)` is a harmless no-op.
None of this business of "I've got to return now, let's see, how many of these buffers do I need to release at this point in time?", with varying amounts of the same free statement appearing redundantly. Write the cleanup block once when the pointers are about to fall out of scope, have it able to run in both success and failure cases and be done with it. Think of it as a more manual RAII if you like.
As for what to replace those early `return`s with, the two common schools are `goto` into the cleanup block, or repeatedly checking some kind of failure status variable before performing new actions.
- Nursie 13y agoWhat you've described looks just as messy to my eyes, and I've seen all different ways to do this. You can nest under if(thing!=NULL) but then you end up with indentation creep. You can use the goto pattern if you like but some folks will tell you that goto's are never, ever to be used. Or you can do what's done above. When it comes down to it the logic is basically identical and it's just down to code formatting.
- seabee 13y agoThe logic may be identical but the scope for programmer error is not. Consider the possible mistakes someone can write by doing it each different way.
- Nursie 13y agoI've seen errors in all the ways considered here.
- asveikau 13y ago> You can nest under if(thing!=NULL) but then you end up with indentation creep. I am not suggesting any nesting of anything. Repeatedly checking at the same indentation level. > You can use the goto pattern if you like but some folks will tell you that goto's are never, ever to be used. What matters more to you, getting stuff right or repeating adages that other people have said out of context? `goto` is probably the cleanest way to do it in plain C. > When it comes down to it the logic is basically identical and it's just down to code formatting. No actually, it is quite a bit more than style, doing it the way alexkus has it is much much much less maintainable when it's done all over a large code base. He's got `foo`, `bar`, `qux`. What if a year later some future maintainer totally unfamiliar with the code needs to add another one? Then it's up to that person to find all of the exit paths, make sure that `foo`, `bar`, `qux` and the new thing are freed in all cases. Doing that is a lot harder if the free statements are all over the place and repeated several times instead of in a single block.
- Nursie 13y ago>> I am not suggesting any nesting of anything. Repeatedly checking at the same indentation level. This could be considered wasteful. You end up checking if something is NULL, if it is you jump to the cleanup code and immediately check again. Nesting may be more elegant. >> What matters more to you, getting stuff right or repeating adages that other people have said out of context? `goto` is probably the cleanest way to do it in plain C. Writing code in a way consistent with the team I work with and the established codebase. >> What if a year later some future maintainer totally unfamiliar with the code needs to add another one? Then they need to read what the function is doing and understand it before they mess with the code, just like in any other situation. >> Doing that is a lot harder if the free statements are all over the place and repeated several times instead of in a single block. More verbose certainly, but if the code is written in small, discrete functional blocks then it shouldn't really impact much. He repeats frees, you repeat tests. An indenter would repeat neither but has indent readability to consider. Frankly, don't trust anyone that tells you that they have the one true way to do things.
- asveikau 13y ago