Since I had read realloc will act as malloc if the size pointed is 0, I was using it without malloc(), provided the pointer was static, global, or explicitly set to NULL if automatic.
However, I notice a lot of programmers try to set it or set it to malloc(1). Is it needed?
malloc
is not required, you can userealloc
only.malloc(n)
is equivalent torealloc(NULL, n)
.However, it is often clearer to use
malloc
instead of special semantics ofrealloc
. It's not a matter of what works, but not confusing people reading the code.(Edit: removed mention of
realloc
acting asfree
, since it's not standard C. See comments.)From Open Groups' specifications: