[or-cvs] Stupid sizeof operator!
Ben Laurie
ben at algroup.co.uk
Thu Apr 8 08:49:47 UTC 2004
Roger Dingledine wrote:
> On Thu, Apr 08, 2004 at 07:41:47AM +0100, Ben Laurie wrote:
>
>>>Log Message:
>>>Stupid sizeof operator!
>>>
>>>- tmp_cpath = tor_malloc_zero(sizeof(tmp_cpath));
>>>+ tmp_cpath = tor_malloc_zero(sizeof(crypt_path_t));
>>
>>Errr ... stupid coder, you mean? You wanted:
>>
>>tmp_cpath = tor_malloc_zero(sizeof *tmp_cpath);
>
>
> No, I think sizeof(crypt_path_t) is a fine way to do it. It makes it
> clearer to me that we're talking about the size of the struct, not the
> size of a pointer or something.
>
> I guess it could be a bother if tmp_cpath changes types, but I don't
> think that's much of a problem.
You can do it either way, my point was that the sizeof operator was not
at fault. I prefer the idiom "x=malloc(sizeof *x)" because you never
have to check what x points to. Indeed some people define a NEW macro
like this:
#define NEW(x) (x)=malloc(sizeof *(x))
Cheers,
Ben.
--
http://www.apache-ssl.org/ben.html http://www.thebunker.net/
"There is no limit to what a man can do or how far he can go if he
doesn't mind who gets the credit." - Robert Woodruff
More information about the tor-dev
mailing list