malloc(0) returns an invalid address

From: JINMEI Tatuya / 職6柑巳6柑達哉 <jinmei_at_isl.rdc.toshiba.co.jp>
Date: Wed, 01 Dec 2004 14:19:15 +0900
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
Received on Wed Dec 01 2004 - 04:19:10 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:23 UTC