Do I need to initiallize(set to 0) memory after calling realloc?

前端 未结 3 1480
不知归路
不知归路 2020-12-19 17:20

I need to implement a simple dynamic array of pointers to a typedef\'ed pointer.
Using realloc each time it\'s requested by the user, the array size will grow by sizeof(

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 17:43

    Going by the definition of realloc()

    realloc(void *p, size_t size) changes the size of the object pointed to by p to size. The contents will be unchanged up to the minimum of the old and new sizes. If the new size is larger, the new space is uninitialized. (from Kernighan and Ritchie)

    You may have to initialize the new space. You can do that when it is decided what kind of data the address pointed to by the void* will hold.

提交回复
热议问题