Little conveniences
Paul Moore
paul.moore at centrify.com
Thu Oct 1 13:29:35 EDT 2009
or even
static inline void k5alloc(size_t size)
{
return calloc(size, 1);
}
since the results is either good or ENOMEM, there not a lot of point in
returning the failure reason
or simply
#define k5alloc(sz) calloc(sz,1)
-----Original Message-----
From: krbdev-bounces at mit.edu [mailto:krbdev-bounces at mit.edu] On Behalf
Of Greg Hudson
Sent: Thursday, October 01, 2009 10:04 AM
To: Jeffrey Hutzelman
Cc: krbdev at mit.edu
Subject: Re: Little conveniences
On Thu, 2009-10-01 at 12:55 -0400, Jeffrey Hutzelman wrote:
> Huh? What's wrong with this?
>
> static inline krb5_error_code k5alloc(size_t size, void **ptrp)
> {
> *ptrp = calloc(size, 1);
> return (*ptrp == NULL) ? ENOMEM : 0;
> }
First and probably foremost, you'd have to cast the address of every
pointer you passed in, or you'd get a compiler warning. That's uglier
than switching around the return pointer and error code.
Second, even with the casts, it wouldn't be correct C. C (the
standardized language) does not assume that all pointer types are
represented equally. When you write "charptr = voidptr;" you give the
compiler an option to add code to convert the pointer representations.
When you cast &charptr to a void ** and assign through that, you do not.
Of course, it works fine in C (the language we actually use on platforms
anyone has ever heard of) because in reality all pointer types are
usually represented equally.
_______________________________________________
krbdev mailing list krbdev at mit.edu
https://mailman.mit.edu/mailman/listinfo/krbdev
More information about the krbdev
mailing list