Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Considering the harmful

Name: Anonymous 2008-04-23 19:14

So when writing C code and you have a function that allocates stuff and other things that needs to be cleaned up, how do you make things not to look like a mess when errors happen? For example (with return 0 for ok, and return 1 on error)

if ((a = AllocA()) == NULL)
    return 1;
if ((b = AllocB()) == NULL) {
    FreeA(a);
    return 1;
}
if (SomeInit() != INIT_OK) {
    FreeA(a);
    FreeB(b);
    return 1;
}
while (something) {
    if (error) {
        FreeA(a);
        FreeB(b);
        SomeCleanup();
        return 1;
    }
}
if (tarballs) {
    FreeA(a);
    FreeB(b);
    SomeCleanup();
    return 1;
}
return 0;

There's a lot of repitiion in that. What I'd really want would be something analogous to atexit() for the current scope. The only way that doesn't repeat all that I've thought of is through goto like

if ((a = AllocA()) == NULL)
    return 1;
if ((b = AllocB()) == NULL)
    goto free_a;
if (SomeInit() != INIT_OK)
    goto free_b;

while (something) {
    if (error)
        goto free_all;
}
if (tarballs)
    goto free_all;
return 0;

free_all: SomeCleanup();
free_b: FreeB(b);
free_a: FreeA(a);
return 1;

What is /prog/'s thoughts on this situation?

Name: Anonymous 2008-04-24 9:39

Plutôt Christophe

Name: Anonymous 2008-04-24 9:39

>>39
us-intl

Name: Anonymous 2008-04-24 9:49

>>39
In Windows, Ctrl+Alt+e
On a Mac, Opt+e followed by another e
In Linux, AltGr+; followed by e

Name: Anonymous 2008-04-24 9:54

>>43

Name: Anonymous 2008-04-24 9:55

im on windows and used altgr+e: éééééééééééééé  ÉÉÉÉÉÉ  óóóó ííí


ééúúííóó
áá

Name: Anonymous 2008-04-24 11:07

h€x my €n€s

Name: Anonymous 2008-04-24 14:04

0123456789öüóqwertzuiopőúasdfghjkléáűíyxcvbnm,.-

Name: Anonymous 2008-04-24 14:43

The >>3 way seemed ok, as it also allows you to return e as an error code for each possible situation that might occur. However there's a lot of extra indentation which I don't like. Like Linux said, ``If you need more than four levels of indentation, you're screwed anyway''.

Name: Anonymous 2008-04-24 21:18

҈  НАХ МY АИUЅ  ҉

Name: Anonymous 2009-03-06 6:16

Have THOUGHT OF PROGRAMS   IN FORTRON AND   IM AN EXPERT   PROGRAMMER who wrote   a fast Fuun   compiler and then   added a crapload   of extentions to   it and broke   existing ones so   that it would   be wonderful if   the Gimp had.

Name: Anonymous 2009-03-06 10:27


Just to later learn.

Name: Anonymous 2011-02-03 4:21

Name: Anonymous 2011-02-04 12:57

Name: Anonymous 2011-02-18 13:26

that's cool and all, but check 'em

Name: Sgt.Kabukiman䖉 2012-05-22 23:19

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List