Skip site navigation (1)Skip section navigation (2)
Date:      9 Apr 2011 14:43:36 -0000
From:      "John Levine" <johnl@iecc.com>
To:        freebsd-questions@freebsd.org
Cc:        kerolasa@gmail.com
Subject:   Re: malloc: errno: 22: Invalid argument
Message-ID:  <20110409144336.72626.qmail@joyce.lan>
In-Reply-To: <BANLkTikEZHkQhLYz9-gEGfJnSoDWSAKFxA@mail.gmail.com>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
>-- snip
>#include <err.h>
>#include <errno.h>
>#include <stdlib.h>
>
>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



Want to link to this message? Use this URL: <http://docs.FreeBSD.org/cgi/mid.cgi?20110409144336.72626.qmail>