Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 01 Dec 2004 14:19:15 +0900
From:      JINMEI Tatuya / =?ISO-2022-JP?B?GyRCP0BMQEMjOkgbKEI=?= <jinmei@isl.rdc.toshiba.co.jp>
To:        current@FreeBSD.org
Subject:   malloc(0) returns an invalid address
Message-ID:  <y7vis7mppd8.wl@ocean.jinmei.org>

next in thread | raw e-mail | index | archive | help
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@isl.rdc.toshiba.co.jp



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?y7vis7mppd8.wl>