[exim] Off-topic coding style (was: [BUG?] in src/auths/pwch…

Top Page
Delete this message
Reply to this message
Author: Michael J. Tubby G8TIC
Date:  
To: Chris Lightfoot, Peter D. Gray
CC: exim-users
Subject: [exim] Off-topic coding style (was: [BUG?] in src/auths/pwcheck.c)

----- Original Message -----
From: "Chris Lightfoot" <chris@???>
To: "Peter D. Gray" <pdg@???>
Cc: <exim-users@???>
Sent: Friday, October 06, 2006 12:57 AM
Subject: Re: [exim] [BUG?] in src/auths/pwcheck.c


> On Fri, Oct 06, 2006 at 09:52:05AM +1000, Peter D. Gray wrote:
>> On Fri, Oct 06, 2006 at 12:32:33AM +0100, Chris Lightfoot wrote:
>> > On Thu, Oct 05, 2006 at 02:28:49PM +0100, Philip Hazel wrote:
>> > the struct is all-bits-zero. That's not necessarily true
>> > (by which I mean, it's almost certainly true on every
>> > platform where exim runs, but the C and POSIX standards
>> > don't require that that be so). The ugly but proper way to
>> > do that is,
>> >
>> >     const struct mime_part mimez = {0};
>> >         /* ... */
>> >     mime_part_p = mimez;

>> >
>> > but only nutters like me actually bother to write their
>> > code this way....
>> >
>>
>> Errrr... what about malloced structures???
>
> struct whatever *w, wz = {0};
> if (!(w = malloc(sizeof *w)))
>    /* handle error */
> *w = wz;

>


Not quite malloc()'d structures there... malloc()'d structure pointers I
think...

Personally I prefer:

struct something_t *sp;
if (sp = zmalloc(sizeof(something_t))) {
    // do something usful
} else {
    // handle error
}


with a local implementation (in library code) of zmalloc that looks like:

void * zmalloc(size_t size)
{
    void * p = malloc(size);
    if (p)
        memset(p, 0, size);
    return p;
}


but I throw structures and pointers to structures around in memory a lot in
my code (usually with double-linked-lists on the front) - I always want to
clear the structure to zero on creation and prefer the "if it worked do
something else handle error" to avoid the double negatives and keep the code
readable

But that's the beauty of C...


Mike