What's the point of malloc(0)?

后端 未结 17 1762
庸人自扰
庸人自扰 2020-11-22 15:40

I just saw this code:

artist = (char *) malloc(0);

...and I was wondering why would one do this?

17条回答
  •  攒了一身酷
    2020-11-22 16:00

    In Windows:

    • void *p = malloc(0); will allocate a zero-length buffer on the local heap. The pointer returned is a valid heap pointer.
    • malloc ultimately calls HeapAlloc using the default C runtime heap which then calls RtlAllocateHeap, etc.
    • free(p); uses HeapFree to free the 0-length buffer on the heap. Not freeing it would result in a memory leak.

提交回复
热议问题