From owner-freebsd-net@FreeBSD.ORG Sat Feb 21 09:06:35 2004 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id D71F016A4CE for ; Sat, 21 Feb 2004 09:06:35 -0800 (PST) Received: from elvis.mu.org (elvis.mu.org [192.203.228.196]) by mx1.FreeBSD.org (Postfix) with ESMTP id D208A43D1D for ; Sat, 21 Feb 2004 09:06:35 -0800 (PST) (envelope-from mux@freebsd.org) Received: by elvis.mu.org (Postfix, from userid 1920) id ABE775C7F2; Sat, 21 Feb 2004 09:06:35 -0800 (PST) Date: Sat, 21 Feb 2004 18:06:35 +0100 From: Maxime Henrion To: Petri Helenius Message-ID: <20040221170635.GL35475@elvis.mu.org> References: <40378E93.3010804@rommon.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <40378E93.3010804@rommon.com> User-Agent: Mutt/1.4.1i cc: FreeBSD - net Subject: Re: malloc (0) X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 21 Feb 2004 17:06:35 -0000 Petri Helenius wrote: > > I?m asking the net list because I came across this when browsing through > the networking / resolver code. > > The question is if the code should check for zero value before calling > malloc or is malloc(0) legal if the pointer is never used? > > I came across this when using dmalloc library and it started complaining > about zero sized allocations from get_addrselectpolicy. In C99, malloc(0) is legal. From n869.txt : %% If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. %% As a side note, free(NULL) is also legal in C99. Cheers, Maxime