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