Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

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