What will be printed out as a result of the following code execution?
#include <vector>
#include <iostream>

struct A {
  A(int i) { std::cout << "A(" << i << ")"; }
  ~A() { std::cout << "~A()"; }
};

int main() {
  std::vector<A *> a;

  for (int i = 0; i < 3; i++) {
    a.push_back(new A(i));
  }
  
  return 0;
}
Explanation
STL containers do not call delete for pointers to objects, so the ~A() destructor will not be called once and there will be a memory leak allocated for objects of class A stored in std::vector

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты