malloc(0) returns an invalid address
JINMEI Tatuya /神明達哉
jinmei at isl.rdc.toshiba.co.jp
Tue Nov 30 21:19:11 PST 2004
On FreeBSD 5.3 Release (for i386), malloc(3) seems to return an
invalid pointer, while the man page says:
V Attempting to allocate zero bytes will return a NULL pointer
instead of a valid pointer. (The default behavior is to make a
minimal allocation and return a pointer to it.) This option is
provided for System V compatibility. This option is incompatible
with the ``X'' option.
In fact, if you compile the following code,
#include <stdlib.h>
#include <stdio.h>
main()
{
char *p = malloc(0);
printf("address of p is %p\n", p);
*p = 0; /* XXX */
}
the result of the execution would be as follows:
% ./a.out
address of p is 0x800
zsh: 794 segmentation fault (core dumped) ./a.out
Is this a malloc bug? Or is this the intended behavior and the man
page description is old?
(For that matter, I don't have /etc/malloc.conf or MALLOC_OPTIONS in
the local environment.)
JINMEI, Tatuya
Communication Platform Lab.
Corporate R&D Center, Toshiba Corp.
jinmei at isl.rdc.toshiba.co.jp
More information about the freebsd-current
mailing list