Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does malloc(0) return a non-null address in Windows?

The code below returns an address when executed in Windows, though I was expecting it to return NULL.

int main()
{
   char *ptr = NULL;
   ptr = malloc(0);
   printf("malloc returned = %u\n", ptr);

}

What could have prompted such an implementation of malloc? Is there any reason behind it?

Since, this is a 0 byte memory, I didn't experiment writing any data. But, can this memory be used for anything at all?

like image 391
Jay Avatar asked Mar 15 '12 15:03

Jay


People also ask

Why does malloc 0 return valid memory address what's the use?

malloc(0) does not allocate any memory. [EDITED: it can sometimes allocate memory, see my next answer] The return value of malloc (0) is implementation specific: it can return NULL or a valid pointer (some unique value) as in your case but memory is not allocated!!!

What will malloc 0 return?

An implementation for which malloc(0) always returns NULL is expressly permitted by the POSIX standard: 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.

How do I force malloc return NULL?

Unless your memory is already completely reserved (or heavily fragmented), the only way to have malloc() return a NULL -pointer is to request space of size zero: char *foo = malloc(0);

What happens when malloc returns NULL?

If the malloc function is unable to allocate the memory buffer, it returns NULL. Any normal program should check the pointers which the malloc function returns and properly handle the situation when the memory allocation failed.


3 Answers

It's just the minimum size you're requesting. And since there are no zero-length blocks in the Win32 heap, you can:

void *p = malloc(0);
// ... do some stuff in between...
realloc(p, n);

Which should mostly result in reusing a block of the heap (if you're lucky and the new size is small). A minor opportunist optimization (or a slow-down, depending on the context and blood coffee-levels).

This is a simplified example. The actual situation could be a class that allocates a buffer when it's created and also allows to grow it. If the inputs are annoying to control, you could just let it do that zero-sized buffer allocation.

like image 139
ActiveTrayPrntrTagDataStrDrvr Avatar answered Oct 26 '22 21:10

ActiveTrayPrntrTagDataStrDrvr


To answer the second part of your question no, the memory can't be used for anything, since "the accessible part of it" is 0 bytes starting from the address returned.

IIRC it is guaranteed though that if the implementation does return non-null, then it will be a different address from any other currently allocated memory block. So in theory you could use the address as a temporary unique ID (of pointer type), although obviously there are plenty of other ways of getting a unique ID. And even if you were going to use it for that, malloc(1) would work just as well as malloc(0) and be more portable.

like image 30
Steve Jessop Avatar answered Oct 26 '22 21:10

Steve Jessop


That's answered within the comp.lang.c FAQ: http://c-faq.com/ansi/malloc0.html

The ANSI/ISO Standard says that it may do either; the behavior is implementation-defined (see question 11.33). Portable code must either take care not to call malloc(0), or be prepared for the possibility of a null return.

like image 34
CAbbott Avatar answered Oct 26 '22 22:10

CAbbott