Re: data structure initialization

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

 



On Fri, Nov 27, 2009 at 6:15 PM, Bernd Petrovitsch <bernd@xxxxxxxxx> wrote:
On Fri, 2009-11-27 at 18:07 +0530, Anuz Pratap Singh Tomar wrote:
> On Fri, Nov 27, 2009 at 5:56 PM, Bernd Petrovitsch <bernd@xxxxxxxxx>
> wrote:
>         On Fri, 2009-11-27 at 17:50 +0530, Anuz Pratap Singh Tomar wrote:
>         [...]
>         > I have came across, this kind of structure initialization for the
>         > first time:
>
>         [...]
>         >         [ICMP_ADDRESSREPLY] = {
>         >                 .output_entry = ICMP_MIB_OUTADDRMASKREPS,
>         >                 .input_entry = ICMP_MIB_INADDRMASKREPS,
>         >                 .handler = icmp_address_reply,
>         >         },
>         > };
>         >
>         >
>         > I am not sure what those indexes are for: for example
>         > [ICMP_ADDRESSREPLY].
>
>         You tell the C compiler that the above element should be at index
>         ICMP_ADDRESSREPLY (which must be a compile-time constant of course and
>         counting starts with 0 - as always in C).
>
> But why is implicit indexing required, it would anyways be indexed
I assume you meant "explicit indexing" (and not implicit)

oops! yes.
>  from 0?
With the above, you guarantee that the above element is always at the
same place (read: the same offset in the array).
It avoids changes (read: bugs and problems) if someone inserts a new
field at that begin of the definition (and you get off-by-1 errors
without a change).

It's probably not the case for above but: Constants may be different on
different architectures (for whatever reason, hardware dictated,
historically grown, compatible to other OSs there, ...). And the above
is an elegant and pretty robust solution without duplicating lots of
source (because each arch defines it on their own somewhere) or
cluttering the source with #ifdef's, assert()s and the like just to make
sure that an array element is at the correct offset.

Thanks, that explains.

[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux