From owner-freebsd-questions@FreeBSD.ORG Sat Apr 9 15:10:41 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 0C1BF106564A for ; Sat, 9 Apr 2011 15:10:41 +0000 (UTC) (envelope-from prvs=0073a8506e=johnl@iecc.com) Received: from gal.iecc.com (gal.iecc.com [64.57.183.53]) by mx1.freebsd.org (Postfix) with ESMTP id A22FD8FC1B for ; Sat, 9 Apr 2011 15:10:40 +0000 (UTC) Received: (qmail 94239 invoked from network); 9 Apr 2011 14:43:59 -0000 Received: from mail1.iecc.com (64.57.183.56) by mail1.iecc.com with QMQP; 9 Apr 2011 14:43:59 -0000 Date: 9 Apr 2011 14:43:36 -0000 Message-ID: <20110409144336.72626.qmail@joyce.lan> From: "John Levine" To: freebsd-questions@freebsd.org In-Reply-To: Organization: X-Headerized: yes Mime-Version: 1.0 Content-type: text/plain; charset=utf-8 Content-transfer-encoding: 7bit Cc: kerolasa@gmail.com Subject: Re: malloc: errno: 22: Invalid argument X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Apr 2011 15:10:41 -0000 >-- snip >#include >#include >#include > >int main(void) >{ > int *i; > warn("errno: %d", errno); > i = malloc(sizeof(int)); > warn("errno: %d", errno); > free(i); > return (errno); >} >-- snip Your code is wrong. There's only a useful value in errno after something fails. This would be more reasonable: int main(void) { int *i; /* warn("errno: %d", errno); -- no error, nothing to check */ i = malloc(sizeof(int)); if(!i)warn("errno: %d", errno); /* only warn on failure */ free(i); /* -- free ignores NULL argument */ return (0); /* -- free cannot fail, no meaningful errno */ } This isn't specific to FreeBSD, by the way. It's ANSI C. Regards, John Levine, johnl@iecc.com, Primary Perpetrator of "The Internet for Dummies", Please consider the environment before reading this e-mail. http://jl.ly