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

>>17
Time for debunking...
I'm going to skip point 1 which is just too damn stupid.
I don't think he's aware that malloc() can fail for a number of reasons, and not just "process out of memory".

Let's start with 2:
2. Casting void-star
Excuse me, but WHO THE FUCK calls void * ``void-star''?
What the FUCK. At that point I thought he was trolling.
It’s the void pointer’s job to stand in for other pointer types.
No, that's a terrible lie. void * is there for a number of reasons, (proper alignment, polymorphism) but it's not for all pointers.
Only for pointers to objects.

In ANSI C99
No, you mean in ISO C99. ANSI wrote C89.
the “cast” is implied
There are implicit conversions, but not implicit (or explicit) casts.
when you pointers to or from void-star
When I pointers to or from? Ok. That made sense.

But the redundant int-star cast isn’t just extra noise. Because the “implicit cast” behavior is unique to void-star, casting explicitly hides an error: if variable types change (say, you add an argument to a function), and what you thought was a void-star ceases to be a void-star, a casted assignment will suppress a
The reason the cast is avoided in C in malloc and friends is because if the prototype is missing compilers will assume the functions return an ``int''. ints are not guaranteed to hold a pointer correctly, and therefore all sorts of problems might arise.
Nothing remotely related to what you are trying to say.
And look askance at ANY pointer cast in straight C code.
I can think of two examples where it is necessary

/* 1 */
char *p = "hello world";
printf("%p\n", (void *)p);
/* 2 */
execl(path, path, arg1, arg2, (char *)0);


Less typing, safer code.
Hah, false premises.

Now, to 3

3. Passing pointers to tiny structures
You want to return a pointer to a buffer and its length. You use the equivalent of:
struct iov {
u_char *base;
size_t len;
};

Well, that's not how iov is declared in POSIX. man 3 readv for details.

On LP32, struct iov is as hard to pass in and out of a function as long long.
In ISO C99, you don't have to pass or return structures by pointer. Since you mentioned long long (which is C99), then on LP32 you can pass structures as is.

Put differently, unless you go out of your way to individually allocate and pass pointers to long long to avoid the “overhead” (use of successive registers) of copying, you don’t get to pass pointers to struct iov.
I'm not sure what you are trying to say, but it's not C.

Dragging the point out further, I could say that if you use long long in your code, you should be passing “pair” structs instead of using out-args to store lengths. But I’m not going to go there, because that’d be hypocritical even if it was correct.

It's not. It invokes undefined behavior. You are a hypocrite anyway. Read the god damn C99 standard (protip: It's from ISO. Not ANSI YOU MORON)

Less typing, safer code.
I'm gonna fucking suicide.

Moral cousin to gripe (2). You can “dereference” an undefined pointer in a “sizeof” expression. So this:

No, you cannot. Try this:
int (*n)[*];
So, maybe you should leave VLAs out. Even then, it's not what you can do. It's what sizeof does. sizeof does not evaluate its operand, with some exception rules for VLAs

As for your malloc example, it's a known idiom.

So tl;dr your cretinous article is full of mistakes, inaccuracies, hypocrisy and fail. http://www.matasano.com/log/consulting-services/
I lold ^. Kids these days.

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