What is C++ version of realloc(), to allocate the new buffer and copy the contents from the old one?

后端 未结 4 1325
北恋
北恋 2020-12-06 10:04

In C we used malloc(), free(), but in C++ youare using new, delete, but in C we also have realloc, which will alloc the new block and

4条回答
  •  鱼传尺愫
    2020-12-06 10:39

    C++ doesn't have a new/delete equivalent of C's realloc.

    The probable reason (although it is not mentioned anywhere in the Standard) is because of constructors that can throw: how should it behave if, while reallocating, a constructor throws? The handling of this case is best left to the programmer because there is not one true answer.

提交回复
热议问题