13

In C, we have malloc(), free(), and realloc(). In C++ we have new(), delete() and their array versions. Is there a C++ realloc function? I'm implementing some low level stuff in embedded land and just realized there was no realloc function to pair up with the C++ functions and wanted to make sure I wasn't missing anything. I'm guessing "placement new" into a new, separate buffer is the closest match, but wanted to be sure.

Restating the question a bit as I'm getting some answer a bit far afield.

I have implemented device level malloc/new/realloc/etc. functions on my embedded device and wanted to double check to be sure there was no C++ realloc type function that I was unaware of.

31
  • 8
    I once asked a similar question. Commented Jun 24, 2013 at 21:23
  • 2
    Any particular reason not to use containers that expand, like for example std::vector? Memory concerns? Commented Jun 24, 2013 at 21:23
  • 1
    @Huytard I think he's asking not whether he could use realloc, but a more C++ way to handle the kinds of situations realloc is meant for. He could also use malloc and lay out his objects as a struct himself, but he'd rather not ;) Commented Jun 24, 2013 at 21:27
  • 2
    @MichaelDorgan: Yeah... basically, the upshot is that there isn't really a need for this in C++. vector already as amortized constant pushback cost, so there isn't really any asymptotic problem to start with. Commented Jun 24, 2013 at 21:34
  • 2
    @KerrekSB On the other hand, modern allocators are capable of distinguishing small from large objects and handling them differently. On Unix-like platforms large objects can be (and on Linux are) allocated with mmap, and reallocated with mremap, in which case there can be a realloc that never copies (large enough chunks). Commented Jun 24, 2013 at 22:58

1 Answer 1

4

No, there is no direct equivalent. You would have to do the implementation yourself. Since a class really shouldn't change its size, this isn't really an issue. Move semantics can handle most of these cases.

However, there are some classes that use header info + tail end allocated data. To code these 'properly' you would have to override the operator new() and operator delete() function for the class to handle this additional 'flexible' data. How you 'reallocate' the data is specific to your needs.

If this doesn't answer your question, post an example of what you are attempting.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice answer, but I believe @KerrekSB above gave me the info I needed. I am, or have already in this case, coded all the low level memory allocation glue code to fit with the standard allocators and I just wanted to be sure there was no C++ realloc equivalent that I was missing. +1 and thanks.
I give you the prize as everyone else has gotten into a C++11 argument :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.