...
Code Block | ||||
---|---|---|---|---|
| ||||
#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 | ||||
---|---|---|---|---|
| ||||
#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 | ||||
---|---|---|---|---|
| ||||
struct S { void f(); }; void fg() { S s; // ... s.f(); } |
...