From owner-freebsd-questions@FreeBSD.ORG Sat Apr 9 15:17:07 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 E021E106564A for ; Sat, 9 Apr 2011 15:17:07 +0000 (UTC) (envelope-from kerolasa@gmail.com) Received: from mail-qy0-f182.google.com (mail-qy0-f182.google.com [209.85.216.182]) by mx1.freebsd.org (Postfix) with ESMTP id 8ED6D8FC16 for ; Sat, 9 Apr 2011 15:17:07 +0000 (UTC) Received: by qyk27 with SMTP id 27so3094505qyk.13 for ; Sat, 09 Apr 2011 08:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:reply-to:in-reply-to :references:date:x-google-sender-auth:message-id:subject:from:to:cc :content-type:content-transfer-encoding; bh=9tX3lXes3Cs1nsRu42clLZaskZChtfrDU6zUuULDx7U=; b=Qii5R6DkMmHFf4HCvUfe6UGuOnAovNQkbDq9VQUzHZ3rL0LxYT6p9MGAVcbjkoruDA vO7SHOfFvvSs9yGFehnjv4yvytVCTLEwQp9m5mNFBRinO1JwaOPzRJWT6IMeeJUOTyal /Salh3y7bA7+gPbRqCZ/7RQb43F25Uu9IR7Jg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:reply-to:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=HehVETr4zKn9lJBl5cxiaDv0j8k7MBsreuh98T+pt5JLKf1yFP+PohB/6sBnL66y4W F6OnBCG/vsFtE9x0M1026YxV4MOUsdue5iFoFmmJDbseN6SCLRoeKb0IG6SgzaI23Amz lRU9O596+FSRpmHJ4cFMEvSh59sEM2Lk3taqY= MIME-Version: 1.0 Received: by 10.229.105.82 with SMTP id s18mr2804067qco.109.1302362226609; Sat, 09 Apr 2011 08:17:06 -0700 (PDT) Sender: kerolasa@gmail.com Received: by 10.229.96.137 with HTTP; Sat, 9 Apr 2011 08:17:06 -0700 (PDT) In-Reply-To: <20110409144336.72626.qmail@joyce.lan> References: <20110409144336.72626.qmail@joyce.lan> Date: Sat, 9 Apr 2011 17:17:06 +0200 X-Google-Sender-Auth: Tw14xcRw80b7yNXiA5rAX33Dlss Message-ID: From: Sami Kerola To: John Levine Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cc: freebsd-questions@freebsd.org Subject: Re: malloc: errno: 22: Invalid argument X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: kerolasa@gmail.com List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Apr 2011 15:17:08 -0000 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 check= */ > =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. Different systems seem to work different ways. Indeed you are right, the behavior of operating system setting errno when malloc is successful is allowed. http://pubs.opengroup.org/onlinepubs/009695399/functions/errno.html http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html 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. --=20 =A0=A0 Sami Kerola =A0=A0 http://www.iki.fi/kerolasa/