f******y 发帖数: 2971 | 1 since there are containers like vector, we can allocate memories at run time
much easier and do not need
to worry about the memory leak. It seems to me the "new","alloc" operators
are no longer necessary in
C++. At least, the programmers do not need to handle them directly at all. But
why they are still there? | p***o 发帖数: 1252 | 2 vector is a library, new is a language feature, how can you
implement vector without new?
Anyway I agree with you that in many cases, new should not be
used directly nowadays.
time
【在 f******y 的大作中提到】 : since there are containers like vector, we can allocate memories at run time : much easier and do not need : to worry about the memory leak. It seems to me the "new","alloc" operators : are no longer necessary in : C++. At least, the programmers do not need to handle them directly at all. But : why they are still there?
| E*****7 发帖数: 128 | 3 For objects that use dynamic memory, new operation must be performed in the constructor to allocate the memory. An object used in containers such as vector must have a constructor either written by the programmer to initialize it or a default constructor generated by the compiler by default. If your program always deals built-in type such as integer in vector where the constructor of int generated by the compiler by default always does its job, you need not worry about dynamic allocation at |
|