How to update other pointers when realloc moves the memory block?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The realloc reference says: The function may move the memory block to a new location, in which case the new location is returned. Does it mean that if I do this: void foo() { void* ptr = malloc( 1024 ); unsigned char* cptr = ( unsigned char* )ptr+256; ptr = realloc( ptr, 4096 ); } then cptr may become invalid if realloc moves the block? If yes, then does realloc signal in any way, that it will move the block, so that I can do something to prevent cptr from becoming invalid? 回答1: Yes, cptr will become invalid as realloc moves the block! And