can realloc move pointer if new size smaller?

后端 未结 6 547
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 20:43

I am wondering whether the C or C++ standard guarantees that a pointer is not changed when realloc is called with a smaller (nonzero) size:

size_t n=1000;
T*         


        
6条回答
  •  抹茶落季
    2020-12-09 20:52

    With realloc, you get absolutely no guarantees about where the memory will live afterwords. I believe that libc's default malloc will only begrudgingly copy memory around, so practically speaking you may be OK. But don't count on it.

提交回复
热议问题