Re: errno in debug vs. release
Re: errno in debug vs. release
- Subject: Re: errno in debug vs. release
- From: "E. Wing" <email@hidden>
- Date: Tue, 7 Feb 2006 19:13:47 -0800
> > *From the malloc man page:
> >
> > The malloc() function allocates size bytes of memory and
> > returns a
> > pointer to the allocated memory. malloc() returns a NULL
> > pointer if
> > there is an error.
>
> This snippet does not say that malloc() will set errno on an error,
> and there is no mention of errno in the man page. Errno is generally
> set by system calls (which malloc isn't). The man pages will
> indicate errno values for specific errors, and what indicates that
> you should check errno.
Maybe I missed part of the thread that states which man page the
snipet comes from, but my man page does mention errno (Tiger
10.4.4/Xcode 2.2.1).
If successful, the malloc(), calloc(), and valloc() functions return a
pointer to allocated memory. If there is an error, they return a NULL
pointer and set errno to ENOMEM.
Furthermore, my Linux man page (Debian Sarge) states that setting
errno to ENOMEM upon failure is part of the Unix98 standard. (The man
page also warns against relying too heavily on this in case you use
custom malloc implementations which may not conform to this.)
> > But malloc *is* supposed to set errno* if the size argument equals
> > zero.
But this is still wrong. According to the ISO C standard, including
C99, malloc(0) is implementation dependent (essentially undefined). So
it's not necessarily an error and errno doesn't need to be set in this
case. So Darwin/Apple are doing the correct thing in this case.
(Interestingly from a Google search, somebody filing a FreeBSD bug
points to a Usenet thread in comp.std.c "Memory question" which comes
to the conclusion that even though malloc(0) is implementation
dependent, it cannot return the same non-NULL pointer for multiple
calls to malloc(0).)
-Eric
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden