Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: code clarification

...

Code Block
bgColor#FFCCCC
langcpp
#include <cstring>

struct S {
  int i, j, k;
 
  // ...

  virtual void f();
};

void f() {
  S *s = new S;
  // ...
  std::memset(s, 0, sizeof(S));
  // ...
  s->f(); // undefined behavior
}

Compliant Solution

In this compliant solution, the data members of S are cleared explicitly instead of calling std::memset():

...