From owner-freebsd-questions@FreeBSD.ORG Sun Apr 10 08:47:13 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0B255106568C for ; Sun, 10 Apr 2011 08:47:13 +0000 (UTC) (envelope-from freebsd@edvax.de) Received: from mx02.qsc.de (mx02.qsc.de [213.148.130.14]) by mx1.freebsd.org (Postfix) with ESMTP id C14588FC12 for ; Sun, 10 Apr 2011 08:47:12 +0000 (UTC) Received: from r55.edvax.de (port-92-195-63-56.dynamic.qsc.de [92.195.63.56]) by mx02.qsc.de (Postfix) with ESMTP id 40AF21D90A for ; Sun, 10 Apr 2011 10:47:11 +0200 (CEST) Received: from r55.edvax.de (localhost [127.0.0.1]) by r55.edvax.de (8.14.2/8.14.2) with SMTP id p3A8lA1t001602 for ; Sun, 10 Apr 2011 10:47:10 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Sun, 10 Apr 2011 10:47:10 +0200 From: Polytropon To: freebsd-questions@freebsd.org Message-Id: <20110410104710.f8c14744.freebsd@edvax.de> In-Reply-To: References: <20110409144336.72626.qmail@joyce.lan> Organization: EDVAX X-Mailer: Sylpheed 2.4.7 (GTK+ 2.12.1; i386-portbld-freebsd7.0) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: malloc: errno: 22: Invalid argument X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 10 Apr 2011 08:47:13 -0000 On Sat, 9 Apr 2011 17:17:06 +0200, Sami Kerola wrote: > On Sat, Apr 9, 2011 at 16:43, John Levine wrote: > > Your code is wrong. =A0There's only a useful value in errno after > > something fails. =A0This would be more reasonable: > > > > int main(void) > > { > > =A0 =A0 =A0 =A0int *i; > > =A0 =A0 =A0 =A0/* warn("errno: %d", errno); -- no error, nothing to che= ck */ > > =A0 =A0 =A0 =A0i =3D malloc(sizeof(int)); > > =A0 =A0 =A0 =A0if(!i)warn("errno: %d", errno); /* only warn on failure = */ > > =A0 =A0 =A0 =A0free(i); =A0 =A0/* -- free ignores NULL argument */ > > =A0 =A0 =A0 =A0return (0); /* -- free cannot fail, no meaningful errno = */ > > } > > > > This isn't specific to FreeBSD, by the way. =A0It's ANSI C. >=20 > Different systems seem to work different ways. Indeed you are right, > the behavior of operating system setting errno when malloc is > successful is allowed. >=20 > http://pubs.opengroup.org/onlinepubs/009695399/functions/errno.html > http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html >=20 > Perhaps it was just a naive beginner expectation that errno is not set > by functions when they are successful. I'll remove the check from the > end of the program since there is no guarantees it would mean anything > sensible. Allow me an addition: For meaningful main() return values, use EXIT_SUCCESS or EXIT_FAILURE (as defined in stdlib.h) instead of the errno set by a function. This is the always portable "test for zero", as in "everything okay -> zero; error -> not zero, usually 1, but primarily NOT ZERO". :-) FreeBSD allows more detailed return values like EX_CONFIG, EX_USGE or EX_SOFTWARE (as defined in sysexits.h), but I'm not sure if this is fully portable across all BSDs, UNIX, and the various Linusi. --=20 Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...