Differences between using realloc vs. free -> malloc functions

前端 未结 5 1004
自闭症患者
自闭症患者 2020-12-23 19:59

Why would one use realloc() function to resize an dynamically allocated array rather than using free() function before calling the malloc() function again (i.e. pros and con

5条回答
  •  旧时难觅i
    2020-12-23 20:34

    The advantage is that realloc will preserve the contents of the memory. With free + malloc you'd need to reset the data in the array.

提交回复
热议问题