Is it OK to create dynamically allocating objects with new keyword ?
for example consider std::vector i.e. pointer = new std::vector if I
add many objects to vector
Yes, there is no special provision about that in the Standard.
A nitpick: there is no type 'std::vector', it's a template. If you need
to create an object, you need a concrete type. For instance, to create
, then it may need to copy itself to
another location of RAM.
Objects cannot copy themselves. 'this' pointer is non-assignable.
then pointer value is no longer correct.
I don't understand what you're trying to say.
Standard containers manage their own dynamic memory. It does not matter
where the object itself lives. The two locations (of the container
object and of its contained items) are independent. Standard vector
keeps its elements in an array allocated in the freestore (using new[]
operator).
how
can I access the object then ?
The same way you always access the object.
If you're experiencing some kind of a problem, get to it. If you're
trying to theorise about what would happen to a vector's storage if the
vector itself moves from one place in memory to another, which is OK if
it's dynamically allocated, then there is nothing to worry about. As I
said before, the storage for the vector's elements and the storage for
the vector itself are *independent*. Moving one does not invalidate the
other. That's how you can have vectors or vectors.
V