From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:41552) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RFyFo-0002pJ-Cd for qemu-devel@nongnu.org; Mon, 17 Oct 2011 21:14:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RFyFn-0004Nv-Ei for qemu-devel@nongnu.org; Mon, 17 Oct 2011 21:14:04 -0400 Received: from auth.b.painless.aaisp.net.uk ([90.155.4.48]:40656) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RFyFn-0004Mq-7C for qemu-devel@nongnu.org; Mon, 17 Oct 2011 21:14:03 -0400 Received: from zubnet.me.uk ([81.187.243.246] helo=circe) by b.painless.aaisp.net.uk with esmtp (Exim 4.72) (envelope-from ) id 1RFyFc-0006st-AX for qemu-devel@nongnu.org; Tue, 18 Oct 2011 02:13:52 +0100 Date: Tue, 18 Oct 2011 02:13:49 +0100 From: Stuart Brady Message-ID: <20111018011349.GA13280@zubnet.me.uk> References: <1318881685-23983-1-git-send-email-sw@weilnetz.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1318881685-23983-1-git-send-email-sw@weilnetz.de> Subject: Re: [Qemu-devel] [PATCH] gdbstub: Fix memory leak List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org On Mon, Oct 17, 2011 at 10:01:25PM +0200, Stefan Weil wrote: > > The patch also slightly cleans the g_malloc0 statement which was > touched by that change (no type cast, easier code review). [...] > - s = (GDBRegisterState *)g_malloc0(sizeof(GDBRegisterState)); [...] > + s = g_malloc0(sizeof(*s)); Is this the preferred style, or should we be using: s = g_new0(GDBRegisterState, 1); We currently seem to have a mix of the two styles. I kinda prefer using g_new() (or g_new0()) since provided the 'count' parameter is correct, it can't really be wrong, whereas using sizeof() has the potential to be buggy, even if it is still trivial to check that the code is okay. I guess I feel g_malloc() would be best reserved for those cases where we're doing something special which might need a little extra care... BTW, I've just been experimenting with Coccinelle and produced the following semantic patch: @@ type T; @@ -(T *)g_malloc(sizeof(T)) +g_new(T, 1) @@ type T; @@ -(T *)g_malloc0(sizeof(T)) +g_new0(T, 1) @@ type T; expression E; @@ -(T *)g_malloc(sizeof(T) * E) +g_new(T, E) @@ type T; expression E; @@ -(T *)g_malloc0(sizeof(T) * E) +g_new0(T, E) I couldn't get the following working: // THIS DOES NOT WORK: @@ type T; identifier I; @@ -T I = g_malloc(sizeof(*I)) +T I = g_new(T, 1) I expect this won't work either: // THIS PROBABLY DOES NOT WORK EITHER: @@ type T; identifier I; @@ -T I = g_new(T, 1) +T I = g_malloc(sizeof(*I)) Cheers, -- Stuart