Skip to content

std::vector::reserve and reducing memory reallocations #539

Description

@Carmelo908

Channel

C++Weekly

Topics

It's said that you shouldn't add elements to a std::vector while iterating it, because it may cause a reallocation and this will cause all iterators to be left dangling. But what if we use std::vector::reserve before creating those iterators to avoid reallocation? what is the frequency with which std::vector has to reallocate itself when adding new elements? If I know my final size before hand, how much of a performance difference will make to call vector::reserve before my operations?

Length

I don't think this would take more than 10 minutes to explain

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions