Source-Changes-D archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: CVS commit: src



On Fri, Jan 27, 2012 at 10:10:21PM +0000, Alexander Nasonov wrote:
> Mindaugas Rasiukevicius wrote:
> > +   pcq = kmem_zalloc(offsetof(pcq_t, pcq_items[nitems]), kmflags);
> 
> I'm not sure that this is a valid use of offsetof. C99 (7.17/3) defines
> it as
> 
>       offsetof(type, member-designator)
> 
> I don't see a definition of member-designator, but the designator is
> defined as
> 
>       designator:
>               [ constant-expression ]
>               . identifier
> 
> nitems in pcq_items[nitems] is not a constant expression.

At least some versions of gcc complain when the result of offsetof()
isn't a compile time constant.

IIRC something in the standard requires the result to be a compile time
constant - so that it can be used to initialise static data [1].
I suspect uses like the above were not thought of when the standard
was written!

You can probably use:
#define offsetof_array(type, member, index) \
    (offsetof(type, member[0]) + (index) * sizeof ((type *)0)->member[0])

But I can't think of a way that doesn't involve casting a pointer.

        David

[1] One common compiler doesn't treat 'offsetof(s, a[10])/4' as a
compile time constant.

-- 
David Laight: david%l8s.co.uk@localhost


Home | Main Index | Thread Index | Old Index