Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^3: Perl Best Practices - Loop Labels

by talexb (Canon)
on Apr 19, 2020 at 19:41 UTC ( #11115807=note: print w/replies, xml ) Need Help??

in reply to Re^2: Perl Best Practices - Loop Labels
in thread Perl Best Practices - Loop Labels

Interesting .. yet I can see a fairly simple way to restructure this C code, without either of the goto statements ..

something * alloc_something(void) { /* Make two malloc requests. Insure both succeed; return allocated memory, if any. Three possible logic paths: 1. First malloc fails, and we are done. 2. First malloc succeeds, second malloc fails: free the first allocated block, and we are done. 3. First and second mallocs succeed, and we are done. */ something * ret = malloc(sizeof(something)); /* Did the first request succeed? */ if (ret != NULL) { ret->another_thing = alloc_another_thing(); /* Did the second request fail? */ if (ret->another_thing == NULL) { free(ret); ret = NULL; } } return ret; }

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re^4: Perl Best Practices - Loop Labels
by jcb (Vicar) on Apr 20, 2020 at 03:34 UTC

    Yes, and that was the pattern I used before finding the "goto out" pattern in the Linux kernel sources.

    For simple cases, there is little difference, but even with two allocations, a problem begins to appear: compare the indentation depth between the two solutions and consider what happens when they are extended to include a third or fourth allocation. The "goto out" pattern also takes up far less vertical space for error handling in the main code path, like the common open ... or die ... idiom in Perl.

    When the error path is also branchless, simply releasing some subset of allocated objects and returning NULL, the "goto out" pattern has the advantage of cleanly grouping the error path into one block (with many entry points) after the main code path, allowing both to be examined separately.

    I should probably include a reminder here that this pattern is useful in C, but Perl has better ways of handling error exits because the runtime manages memory, unlike in C. I am unsure if I have ever used goto LABEL in Perl.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11115807]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-01-21 08:17 GMT
Find Nodes?
    Voting Booth?