Date: Wed, 2 Nov 2011 17:42:46 -0700 From: mdf@FreeBSD.org To: Alexander Best <arundel@freebsd.org> Cc: freebsd-hackers@freebsd.org Subject: Re: question regarding style(9) and field initialisers in structs Message-ID: <CAMBSHm8THuoCL3Eh7OfD-pNfj_rPihJzkXWD7QncZw7F8BwVXA@mail.gmail.com> In-Reply-To: <20111102235607.GA61095@freebsd.org> References: <20111102235607.GA61095@freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Nov 2, 2011 at 4:56 PM, Alexander Best <arundel@freebsd.org> wrote: > i sent the following message to freebsd-quaestions@ and got no answer. my= be it > is better suited for freebsd-hackers@. > > hi there, > > i found hundreds of the following cases in the FreeBSD src: > > [...] > struct periph_driver { > =A0 =A0 =A0 =A0periph_init_func_t =A0 =A0 =A0init; > =A0 =A0 =A0 =A0char =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0*driver_name; > =A0 =A0 =A0 =A0TAILQ_HEAD(,cam_periph) units; > =A0 =A0 =A0 =A0u_int =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 generation; > =A0 =A0 =A0 =A0u_int =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 flags; > =A0 =A0 =A0 =A0#define CAM_PERIPH_DRV_EARLY =A0 =A00x01 > }; > [...] > static struct periph_driver dadriver =3D > { > =A0 =A0 =A0 =A0dainit, "da", > =A0 =A0 =A0 =A0TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0 > }; > > ...is it proper programming practice to forget about the last field, if i= t > would have been initialised to 0? It's more likely that, in this instance, the field was added after the initializer. Without named initializers, all fields until the last non-zero one need to be explicitly present. style(9) doesn't address it, having been written too long ago, but IMO initializations in a C translation unit [1] should use C99's named initializer feature, to protect against future member re-ordering. And it would be style(9) compliant to leave out any field whose initial value is zero (though sometimes it's good to list it anyways to make it clear that 0 was the desired value). [1] Note that C++ doesn't support C99's named initializer syntax (even in C++0x, which is a bit silly), so any struct initializers in a header file like <sys/malloc.h> must use old-style, since FreeBSD should continue to support writing kernel modules in C++. Cheers, matthew
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAMBSHm8THuoCL3Eh7OfD-pNfj_rPihJzkXWD7QncZw7F8BwVXA>