From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:53603) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SYzN4-0007yk-AG for qemu-devel@nongnu.org; Mon, 28 May 2012 08:48:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SYzMx-0004TA-R3 for qemu-devel@nongnu.org; Mon, 28 May 2012 08:48:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:14393) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SYzMx-0004Sk-Im for qemu-devel@nongnu.org; Mon, 28 May 2012 08:48:19 -0400 Date: Mon, 28 May 2012 13:48:11 +0100 From: "Daniel P. Berrange" Message-ID: <20120528124811.GM19909@redhat.com> References: <1337721812-30229-1-git-send-email-sw@weilnetz.de> <4FBD026C.5040304@weilnetz.de> <4FBD0317.2040507@redhat.com> <4FBD0A40.6040206@weilnetz.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 1.1] virtio: Fix compiler warning for non Linux hosts Reply-To: "Daniel P. Berrange" List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Stefan Hajnoczi Cc: Kevin Wolf , Stefan Weil , Anthony Liguori , qemu-devel@nongnu.org, Paolo Bonzini On Mon, May 28, 2012 at 01:39:58PM +0100, Stefan Hajnoczi wrote: > On Wed, May 23, 2012 at 5:03 PM, Stefan Weil wrote: > > Am 23.05.2012 17:32, schrieb Kevin Wolf: > > > >> Am 23.05.2012 17:29, schrieb Stefan Weil: > >>> > >>> Am 23.05.2012 10:09, schrieb Stefan Hajnoczi: > >>>> > >>>> On Tue, May 22, 2012 at 10:23 PM, Stefan Weil =C2=A0= wrote: > >>>>> > >>>>> The local variables ret, i are only used if __linux__ is defined. > >>>>> > >>>>> Signed-off-by: Stefan Weil > >>>>> --- > >>>>> =C2=A0 hw/virtio-blk.c | =C2=A0 =C2=A04 +++- > >>>>> =C2=A0 1 file changed, 3 insertions(+), 1 deletion(-) > >>>> > >>>> The #ifdef __linux__ further down in the function declares the loc= al > >>>> hdr variable. =C2=A0We could move ret and i down into the #ifdef i= nstead of > >>>> adding a new one. > >>> > >>> I noticed that, but declaring variables anywhere is C++, not C code. > >> > >> It's called C99. > >> > >> Kevin > >> > > > > Maybe, but I already had patches rejected because of that style. > > Did this policy change? I'd appreciate that! >=20 > Agreed, people have been asked to declare variables at the beginning > of the scope. I don't understand why, C99 allows them to be declared > anywhere and it usually makes the code more readable IMO (you don't > have to jump to the definition to check a variable's type). > > What's the problem with C99-style variable declarations anywhere in a f= unction? Not an issue in this particular patch, but declaration at point of first use can cause you problems if the code uses 'goto' alot (eg for return cleanup). eg void somefunc(void) { ...some code... if (...something bad...) goto cleanup; ... some code... char *bar =3D NULL; bar =3D strdup("Hello World"); ...some code... cleanup: free(bar); } In that 'cleanup' label 'bar' will potentially be uninitialized, because it is in scope, but the line where initialization takes place may never been reached. So if you do decide to make use of C99 style decl at first use, then be sure to turn on the GCC warnings to detect these variable initialization problems. Regards, Daniel --=20 |: http://berrange.com -o- http://www.flickr.com/photos/dberrange= / :| |: http://libvirt.org -o- http://virt-manager.or= g :| |: http://autobuild.org -o- http://search.cpan.org/~danberr= / :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vn= c :|