Creating an object on the heap without new

In C ++, is it possible to create an object on the heap without using new or malloc ?

I think that if I use an STL container like vector , it will be put in a heap. If I do this:

 vector<Object> listObjs = vector<Object>(); Object x = Object(...); ... listObjs.push_back(x); 

Where are the objects created here created?

+4
source share
3 answers

The object indicated by x is on the stack. vector::push_back will copy it to the heap.

The allocator object inside vector is likely to be implemented using new or malloc , although it is possible that it uses a different low-level API. For example, both Unix and Windows offer memory matching APIs, which in turn can be used to implement malloc , new and allocators.

+7
source

std::vector does new there.

0
source

From C # / java?

std::vector<Object> listObjs; create a container on the stack that manages the object allocation array on the heap

listObjs.push_back(Object()); select an array of size + 1, copy the old array to the new one and assign Object () to the last

0
source

Source: https://habr.com/ru/post/1342046/


All Articles