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
Зареєструйся Зараз
або Підпишись на майбутні тести