Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: s/::f()/::g()/g;

...

Code Block
bgColor#FFCCCC
langcpp
#include <new>
 
struct S {
  void f();
};
 
void fg() noexcept(false) {
  S *s = new S;
  // ...
  delete s;
  // ...
  s->f();
}

The function fg() is marked noexcept(false) to comply with MEM52-CPP. Detect and handle memory allocation errors.

...

Code Block
bgColor#ccccff
langcpp
#include <new>

struct S {
  void f();
};

void fg() noexcept(false) {
  S *s = new S;
  // ...
  s->f();
  delete s;
}

...

When possible, use automatic storage duration instead of dynamic storage duration. Since s is not required to live beyond the scope of fg(), this compliant solution uses automatic storage duration to limit the lifetime of s to the scope of fg():

Code Block
bgColor#ccccff
langcpp
struct S {
  void f();
};

void fg() {
  S s;
  // ...
  s.f();
}

...