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

后端 未结 2 903
离开以前
离开以前 2020-12-15 04:15

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

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 04:37

    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.

提交回复
热议问题