Does this code contain an error (compilation, execution, or undefined behavior)?
struct A {
  int *i;
  A() : i(new int(0)) { }
  ~A() { delete i; }
};

int main() {
  A a;
  A b = a;
  return 0;
}
Explanation
There is a "double delete bug" because b is copied byte by byte from a using the automatically generated copy constructor, after which the destructor ~A() is called twice for the same variable.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes