What would realloc do if there is no sequential space of memory?

后端 未结 5 1599
一向
一向 2020-12-31 10:30

realloc is used to reallocate the memory dynamically.

Suppose I have allocated 7 bytes using the malloc function and now I want to extend i

5条回答
  •  旧时难觅i
    2020-12-31 10:59

    From the man page:

    realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr, or NULL if the request fails.

    So in other words, to detect failure, just check whether the result was NULL.

    EDIT: As noted in the comment, if the call fails, the original memory isn't freed.

提交回复
热议问题