Skip to content

Uses of Dynamic allocation on vector(iox::cxx::vector) ? #1772

Answered by elfenpiff
givesun asked this question in Q&A
Discussion options

You must be logged in to vote

@givesun Here you are mistaken. new has in the end to options:

  • allocate memory on the heap and create an object at the heap memory location.
  • create an object at the provided memory location

The difference is subtle but in this case we provide the memory location in the first bracket (&at(m_size++)) and then call the constructor of T with T(std::forward(args)...) which results in

new (&at(m_size++)) T(std::forward(args)...);

If we would allocate this on the heap the call would look like:

new T(std::forward(args)...);

If you would like to dive deeper into placement new take a look at this article: https://en.cppreference.com/w/cpp/language/new#Placement_new

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@givesun
Comment options

Answer selected by givesun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants