[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: Review of sizeof usage

From: Daniel Shahaf <d.s_at_daniel.shahaf.name>
Date: Tue, 11 Aug 2015 22:31:11 +0000

Branko Čibej wrote on Tue, Aug 11, 2015 at 21:55:48 +0200:
> On 11.08.2015 17:02, Philip Martin wrote:
> > Stefan Fuhrmann <stefan.fuhrmann_at_wandisco.com> writes:
> >
> >> way we use sizeof. In my opinion, we should take the
> >> size of the created or processed variable instead of its
> >> type, i.e.
> >>
> >> abc_t *v = apr_pcalloc(pool, sizeof(*v));
> >> apr_hash_set(hash, key, sizeof(*key), y);
> >> z = apr_hash_get(hash, key, sizeof(*key));
> >>
> >> rather than
> >>
> >> abc_t *v = apr_pcalloc(pool, sizeof(abc_t));
> >> apr_hash_set(hash, key, sizeof(key_t), y);
> >> z = apr_hash_get(hash, key, sizeof(key_t));

Both of these variants are redundant. We could encapsulate the
redundancy in a macro:

    #define SVN__CALLOC(obj, pool) do { (obj) = apr_pcalloc((pool), sizeof(*(obj))); } while (0)
    {
      abc_t *v;
      SVN__CALLOC(v, pool);
    }

> > We have had problems with both styles in the past, so neither is immune
> > to bugs. I prefer the explicit type as it is easier to grep.
>
> The explicit type form is more accident-prone than the variable form
> because any change requires two modifications in the same statement
> instead of one.

Why doesn't the compiler or buildbot catch accidents?
Received on 2015-08-12 00:31:30 CEST

This is an archived mail posted to the Subversion Dev mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.