Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 9 Apr 2011 17:17:06 +0200
From:      Sami Kerola <>
To:        John Levine <>
Subject:   Re: malloc: errno: 22: Invalid argument
Message-ID:  <>
In-Reply-To: <20110409144336.72626.qmail@joyce.lan>
References:  <> <20110409144336.72626.qmail@joyce.lan>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
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.

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

=A0=A0 Sami Kerola

Want to link to this message? Use this URL: <>