I am writing an application in c in which I'm allocating the memory needed for some arrays dynamically. I know that many of those arrays will need zero allocation. So, will there be a problem when I call free on them? egfree(malloc(0 * sizeof(int)));
? I know my application compiles and runs ok on my machine, but can I rely on this? Cheers!

- 649
- 1
- 7
- 21
-
1Do be aware that `malloc(0)` is [implementation defined](http://stackoverflow.com/a/2132318/922184). – Mysticial Jan 10 '12 at 18:19
-
@Mysticial: Yes, but you can `free` any result it returns (either a valid pointer or NULL), right? – Fred Larson Jan 10 '12 at 18:21
-
What does this have to do with zero-initialization of arrays? – Fred Foo Jan 10 '12 at 18:21
-
@FredLarson Correct, but if `malloc(0)` returns `NULL`, it could trigger out-of-memory checks that check for `NULL`. – Mysticial Jan 10 '12 at 18:22
-
@larsmans: Who said anything about zero initialization? – Fred Larson Jan 10 '12 at 18:23
-
@FredLarson: the OP did, in the question. – Fred Foo Jan 10 '12 at 18:24
-
@larsman: No, he referred to "zero allocation", which I take to mean an allocation of zero size, hence the question. – Fred Larson Jan 10 '12 at 18:25
-
1@FredLarson: oh, sorry, misread it. My eyes must be getting tired. – Fred Foo Jan 10 '12 at 18:26
-
Related: https://stackoverflow.com/questions/61876053/why-does-malloc0-cause-a-major-memory-leak-on-windows (says that on Windows, `free` after `malloc(0)` is mandatory to avoid memory leak) – mic Dec 22 '20 at 13:44
4 Answers
You can safely free(malloc(0))
.
from man malloc
:
If
size
is0
, thenmalloc()
returns eitherNULL
, or a unique pointer value that can later be suc‐ cessfully passed tofree()
.
free:
If
ptr
isNULL
, no operation is performed

- 6,768
- 2
- 27
- 35
-
Ooops! So I can call `free()` safely but, when I call `malloc()` I do check for the result too like: `if(!(arr = malloc(0))){ start screaming!}`... If `malloc(0)` is implementation dependent, this will fail, right? – Stamoulohta Jan 10 '12 at 18:26
-
1Yeah, if you think `malloc()` returning `NULL` is an error then you can't call `malloc(0)`. `malloc()` will set `errno` to `ENOMEM` on failure, so you can check that instead though. – sverre Jan 10 '12 at 18:29
-
1To use `errno` here, you'd have to set it to 0 before calling `malloc`, and even then, it would be less reliable than I'd like. A better test would be just checking whether the size argument was 0 after `malloc` returns null, in which case it's not a failure. – R.. GitHub STOP HELPING ICE Jan 10 '12 at 18:46
malloc(0)
may (this is implementation-defined) return a null pointer, or may return a new pointer each time you call it. In either case, the return value is valid to pass to free
, since free(0)
is a no-op anyway. However, due to some nasty issues with detecting and handling failure of realloc
and disagreements between ISO C and POSIX, I would strongly advise you never to pass size 0 to malloc
or realloc
. You can always simply add +1
or |1
to the end of the argument to malloc
, and then you're certain to get a unique pointer (different from the null pointer or the address of any other object) every time you call it.
Another reason not to use malloc(0)
is that you have to always check the return value of malloc
to detect failure, whereas malloc(0)
can return a null pointer on success. This makes all of your error checking code more complicated since you have to special-case the non-error case where the size argument was 0.

- 208,859
- 35
- 376
- 711
It is always legal to call free
on the result of malloc
(once).

- 137,896
- 35
- 246
- 299
Yes, free(malloc(0))
is guaranteed to work.
For a further discussion, see what's the point in malloc(0)?