How does realloc know how much to copy?

后端 未结 5 1101
庸人自扰
庸人自扰 2020-12-11 15:52

how does realloc know the size of original data?

 void *realloc(void *ptr, size_t size);

So, if the implementation is like this:

         


        
5条回答
  •  旧时难觅i
    2020-12-11 16:22

    realloc (and malloc and free) have full access to the entire datastructure that makes up the heap. In that datastructure is information about the sizes of blocks, which realloc needs to know, and so does free.

提交回复
热议问题