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 1:23

>>24
oh btw your example has a memory leak when malloc the second time that it's called returns NULL. You fail big time.

Name: Anonymous 2008-04-24 1:27

>>26-1000

All trolled by >>24

Name: Anonymous 2008-04-24 1:29

>>26
It's not a leak, it's freeing a NULL pointer. Also, YHBT.

Name: Anonymous 2008-04-24 1:39

>>28
No, I was refering to you NOT free()'ing "a".
You free b which is NULL, and free(NULL) is defined and does nothing. but you don't forget a.
As I said, memory leak. You suck dick, and no, I haven't been trolled. You failed.

Name: Anonymous 2008-04-24 1:41

>>29
What's the usefulness of freeing a NULL pointer? YHBT twice.

Name: Anonymous 2008-04-24 2:20

What's the usefulness
Philosophical questions now?
 
free()  frees  the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc() or realloc().  Otherwise, or if free(ptr) has
already been called before, undefined behaviour occurs.  If ptr is NULL, no operation is performed.

You forget to free "a".

Name: Anonymous 2008-04-24 3:09

==NULL considered verbose. Use ! instead.

Name: Anonymous 2008-04-24 5:17

As an EXPERT PROGRAMMER I much prefer >>3 to >>1 or >>24.

Name: Anonymous 2008-04-24 5:29

>>33
And I prefer drinking piss than eating shit but that doesn't mean I'm going to do any of those.

Name: Anonymous 2008-04-24 5:33

>>34
You've tried both?

Name: Anonymous 2008-04-24 5:50

Im totally learning python or ruby. C is so passe`

Name: Anonymous 2008-04-24 6:26

>>36
Passé!

The accent is acute, not grave.

Name: Anonymous 2008-04-24 7:44

Christophér

Name: Anonymous 2008-04-24 9:31

>>37
sorry, i dont have a faggot keyboard

Name: Anonymous 2008-04-24 9:36

>>39
Touché.

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