32

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 copy the old data (common minimum) and then free the old data bock. So what is the C++ version of that? I can write my own of course, but is there a builtin thing?

main() {
  int i; char *x = malloc(3);
  x[0] = 10;
  x[1] = 20;
  x[2] = 30;
  realloc(x, 4);
  x[3] = 40;
  for (i = 0; i < 4; i++) printf("%i\n", x[i]);
}
exebook
  • 32,014
  • 33
  • 141
  • 226
  • 1
    There is no equivalent of realloc in C++. – piokuc May 23 '13 at 13:16
  • 7
    I'd use a std::vector for that – djf May 23 '13 at 13:18
  • Does anyone know if any std::vector implementations use C memory handling for POD types? – Ross Hemsley Nov 19 '13 at 16:34
  • @RossHemsley I know! It doesn't. I made a simple wrapper around the STL allocator that simply gets the normal functions from it called, but prints info about how they're called. And when a vector reallocates, it allocates new data, copies the elements, destroys the old elements, and lastly deallocates the old data. (If the realloc is caused by a push_back operation, the new element is constructed in the new data before old elements get copied over.) Do note that this is on Windows 10 with MSVC, behavior may differ elsewhere. – Poke Mar 10 '21 at 16:54

4 Answers4

44

There's no new/delete equivalent of realloc in C++.

From Bjarne Stroustrup's FAQ :

Why doesn't C++ have an equivalent to realloc()?

If you want to, you can of course use realloc(). However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember that contrary to naive expectations, realloc() occasionally does copy its argument array. In C++, a better way of dealing with reallocation is to use a standard library container, such as vector, and let it grow naturally.

If you want a resizeable container, just use std::vector, otherwise stay with malloc, realloc and free.

And, to answer your last question, the closest C++ version of your code would be:

main() {
    std::vector<char> x(3);
    x[0] = 10;
    x[1] = 20;
    x[2] = 30;
    x.resize(4);
    x[3] = 40;
    for (int i = 0; i < 4; i++) std::cout << x[i] << std::endl;
}

But that is not the standard way of using a vector and provide little benefits versus simply:

main() {
    std::vector<char> x;
    x.push_back(10);
    x.push_back(20);
    x.push_back(30);
    x.push_back(40);
    for (int i = 0; i < 4; i++) std::cout << x[i] << std::endl;
}
zakinster
  • 10,508
  • 1
  • 41
  • 52
24

Let's see what Bjarne Stroustrup thinks!

Why doesn't C++ have an equivalent to realloc()?

If you want to, you can of course use realloc(). However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember that contrary to naive expectations, realloc() occasionally does copy its argument array.

In C++, a better way of dealing with reallocation is to use a standard library container, such as vector, and let it grow naturally.

masoud
  • 55,379
  • 16
  • 141
  • 208
4

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.

syam
  • 14,701
  • 3
  • 41
  • 65
4

realloc isn't used in C++ because C++ wants to use its copy and default constructors and destructors for things like this in general. But if you got plain old types that you want to handle as fast as possible, there is no reason not to roll your own array class that does what you want. I'm surprised there isn't one that's mostly STL-compatible already in the top Google page.

Alexei Averchenko
  • 1,706
  • 1
  • 16
  • 29