How to update other pointers when realloc moves the memory block?

后端 未结 5 716
既然无缘
既然无缘 2020-12-10 11:41

The realloc reference says:

The function may move the memory block to a new location, in which case the new location is returned.

5条回答
  •  鱼传尺愫
    2020-12-10 12:15

    This is coming a bit late, but the solution to this problem (which nobody has mentioned) is not to use pointers into allocated blocks that will need to be allocated. Instead, use integer-valued offsets from the base pointer or (better) use a struct type and member elements to address specific locations in the allocated object.

提交回复
热议问题