From owner-freebsd-hackers@FreeBSD.ORG Thu Nov 3 00:42:48 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0B80D106564A for ; Thu, 3 Nov 2011 00:42:48 +0000 (UTC) (envelope-from mdf356@gmail.com) Received: from mail-yw0-f54.google.com (mail-yw0-f54.google.com [209.85.213.54]) by mx1.freebsd.org (Postfix) with ESMTP id C07DA8FC0A for ; Thu, 3 Nov 2011 00:42:47 +0000 (UTC) Received: by ywt32 with SMTP id 32so983924ywt.13 for ; Wed, 02 Nov 2011 17:42:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=5VH+94CoHAqH3NkM9K2YLqAKqsF992qu1viIwDU/PqY=; b=cXAp5iJ6S4RXmGm5cEJdaKEY+sDjLHeaT+K7k2133SUcS1nMUyQ1/wulDvkhWhjUGH rOEy5TbVnZ+mxb/Y4poCE8nLdA4Xpvde22ZXQezk66oVgLOwgNQfxo5nOsuF4/8uwa6m 9E0u1IjFy8N+0+UdTgmZ96azHGzS2rLDwe8co= MIME-Version: 1.0 Received: by 10.68.72.33 with SMTP id a1mr8079581pbv.44.1320280966476; Wed, 02 Nov 2011 17:42:46 -0700 (PDT) Sender: mdf356@gmail.com Received: by 10.68.57.40 with HTTP; Wed, 2 Nov 2011 17:42:46 -0700 (PDT) In-Reply-To: <20111102235607.GA61095@freebsd.org> References: <20111102235607.GA61095@freebsd.org> Date: Wed, 2 Nov 2011 17:42:46 -0700 X-Google-Sender-Auth: a0by_CVEovNSY5IcOTHGyK8ga3g Message-ID: From: mdf@FreeBSD.org To: Alexander Best Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-hackers@freebsd.org Subject: Re: question regarding style(9) and field initialisers in structs X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Nov 2011 00:42:48 -0000 On Wed, Nov 2, 2011 at 4:56 PM, Alexander Best 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 must use old-style, since FreeBSD should continue to support writing kernel modules in C++. Cheers, matthew