4 ms·
Your example combined with the parents observation show that C++ put under the same construct the concepts that should be separated: memory allocation should be
by acqq 8y ago
Your example combined with the parents observation show that C++ put under the same construct the concepts that should be separated: memory allocation should be handled differently from the construction, destruction and other resource allocation.
- gpderetta 8y agoMemory allocation/deallocation in C++ is handled separately from construction and destruction. The delete and new syntax is short hand for combining the two.
- naasking 8y ago> memory allocation should be handled differently from the construction, destruction and other resource allocation. These other resources still need an in-memory representation to track and reference resources, so you can't really separate them.
- kccqzy 8y agoMemory allocation and deallocation on the heap basically mean calling the `operator new` and `operator delete` functions in C++. The language provides a default implementation but you can override it. Constructors are orthogonal. The job of a constructor is to construct your object given that the space for the object is already allocated. This could be on the stack, where allocation means bumping the stack pointer, or in-place in preallocated storage (like std::vector), or the result of calling `operator new`. Simply using the `new` syntax does both as a shorthand. Similarly the job of a destructor is to destruct your object without deallocating it. One can in-place destruct without deallocating, or destruct and then deallocate implicitly when the stack pointer is adjusted, or not at all. The `delete` syntax does both destruction and deallocation as a convenience.