Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Rolling back some changes that were incorrect

...

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

The function s::f() 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 S::f() noexcept(false) {
  S *s = new S;
  // ...
  s->f();
  delete s;
}

Compliant Solution (Automatic Storage Duration)

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

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

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

...