On Thu, Jul 26, 2012 at 10:28 AM, John Reiser <jrei...@bitwagon.com> wrote:
>>> I'm using valgrind 3.8.0.SVN from MacPorts on Mac OS X 10.7.4.
>>> valgrind is showing a memory leak of 18 bytes when I try to allocate
>>> an empty array using posix_memalign.  The leak warning is attached.
>>>
>>> The posix_memalign call is
>>>
>>>     void* data;
>>>     const size_t alignment = max(sizeof(T),sizeof(void*));
>>>     if (posix_memalign(&data,alignment,sizeof(T)*size))
>>>         throw bad_alloc();
>>>
>>> where sizeof(T)==64 and size==0.  I've confirmed that I call free on
>>> the same pointer.  If I replace the code with
>>>
>>>     void* data = malloc(sizeof(T)*size);
>>>
>>> the leak warning goes away.  Any idea what could be going wrong?
>
> There is a dispute about the meaning of memory allocation when
> the requested size is 0.  The recipient must not access any memory
> there; after all, the requested size is zero.  Also, NULL strings
> occur *everywhere*, so *any* address ought to be satisfactory
> for a zero-length block.
>
> However, some users were in the habit of using malloc(0) as an
> implementation of the LISP (gensym).  The standard grudgingly
> accommodated those users.  That was a mistake.  The result is
> that most implementations allocate 1 byte when you ask for zero.

Wow, that's suitably insane.  Though I suppose it's related to the
C/C++ ban on zero size structs and no two structs sharing the same
memory address.

This is still a bug in Mac's posix_memalloc, though, since presumably
any craziness they decide to return should still be a valid argument
to free.  I filed a bug with Apple and worked around it in my code
with a !size check.

Geoffrey

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to