[ros-dev] [ros-diffs] [greatlrd] 22195: make malloc, calloc and some other function return NULL if size is 0

Gunnar Dalsnes hardon at online.no
Sat Jun 3 18:16:15 CEST 2006


Was the changes made to fix something? If not: don't fix whats not broken.

MSDN: The return value is NULL if the size is zero and the buffer 
argument is not NULL, or if there is not enough available memory to 
expand the block to the given size. In the first case, the original 
block is freed. In the second, the original block is unchanged.

Magnus Olsen wrote:
> Maybe we should add free to realloc and expand
> to avoid memmory leak ??

NO!!!!!!!!!!!!! MSDN says the memory will not be freed if the call 
fails, so the caller is free to continue to use the memory for whatever 
he likes.

> 
> I know realloc fail in windows it can create memory leak

MSDN: The return value is NULL if the size is zero and the buffer 
argument is not NULL, or if there is not enough available memory to 
expand the block to the given size. In the first case, the original 
block is freed. In the second, the original block is unchanged.

So, if realloc fails in windows it WILL ALWAYS create a memory leak 
(unless you free the memory urself offcourse:-)

> 
> text from internet how malloc should work

With Internet do you mean MSDN? Every implementation of malloc behave 
different. We only want it to behave like the Windows version...

> 
> " If the size of the space requested is 0, the behavior is
> implementation-defined:
>   the value returned shall be either a null pointer or a unique pointer."
> 

MSDN: If size is 0, malloc allocates a zero-length item in the heap and 
returns a valid pointer to that item.

> 
> But most devloper assume malloc will return NULL if size is 0
>  But some windows platfroms it return vaild pointer.
> 
> The question is how should we handle it in reactos,
> return NULL or vaild pointer  for this case ?
> 

Don't try to be smart, just do it like it says on MSDN.

BTW: The original code looks correct to me.

> 
> 
> ----- Original Message ----- 
> From: "Thomas Weidenmueller" <w3seek at reactos.com>
> To: "ReactOS Development List" <ros-dev at reactos.org>
> Sent: Saturday, June 03, 2006 4:39 PM
> Subject: Re: [ros-dev] [ros-diffs] [greatlrd] 22195: make malloc, calloc and
> some other function return NULL if size is 0
> 
> 
>> Royce Mitchell III wrote:
>>>> @@ -60,6 +66,9 @@
>>>>  */
>>>> void* realloc(void* _ptr, size_t _size)
>>>> {
>>>> +   if ( _size == 0)
>>>> +       return NULL;
>>>> +
>>>>    if (!_ptr) return malloc(_size);
>>>>    if (_size) return HeapReAlloc(hHeap, 0, _ptr, ROUND_SIZE(_size));
>>>>    free(_ptr);
>>>> @@ -71,6 +80,9 @@
>>>>  */
>>>>
>>>>
>>> umm... shouldn't we call free() if we're going to return NULL because
>>> size is 0?
>> realloc never frees the memory if it fails.
>>
>>> Also, the free() at the bottom is never called now.
>> it's wrong and shouldn't even be there ;)
>>
>>>> void* _expand(void* _ptr, size_t _size)
>>>> {
>>>> +   if ( _size == 0)
>>>> +       return NULL;
>>>> +
>>>>    return HeapReAlloc(hHeap, HEAP_REALLOC_IN_PLACE_ONLY, _ptr,
> ROUND_SIZE(_size));
>>>> }
>>>>
>>>>
>>> Again, shouldn't we free the existing buffer if we're returning NULL
>>> because size is 0?
>> no, same as with realloc.
>>
>> - Thomas
>> _______________________________________________
>> Ros-dev mailing list
>> Ros-dev at reactos.org
>> http://www.reactos.org/mailman/listinfo/ros-dev
> 
> _______________________________________________
> Ros-dev mailing list
> Ros-dev at reactos.org
> http://www.reactos.org/mailman/listinfo/ros-dev
> 
> 
> 


More information about the Ros-dev mailing list