Is a malloc() needed before a realloc()?

21,917

From Open Groups' specifications:

If ptr is a null pointer, realloc() shall be equivalent to malloc() for the specified size.

If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if the space has previously been deallocated by a call to free() or realloc(), the behavior is undefined.

Share:
21,917
j riv
Author by

j riv

Updated on December 19, 2020

Comments

  • j riv
    j riv over 3 years

    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?