...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <new> void f() { char c; // Used elsewhere in the function unsigned char buffer[sizeof(long)]; long *lp = ::new (buffer) long; // ... } |
Compliant Solution (
...
alignas
)
This In this compliant solution ensures that the long
is constructed into a buffer of sufficient size and with suitable alignment, the alignas
declaration specifier is used to ensure the buffer is appropriately aligned for a long
:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <new> void f() { char c; // Used elsewhere in the function std::aligned_storage<sizeofalignas(long), alignof unsigned char buffer[sizeof(long)>::type buffer]; long *lp = ::new (&buffer) long; // ... } |
Compliant Solution (
...
std::aligned_storage
)
In this This compliant solution , the alignas
declaration specifier is used to ensure the buffer is appropriately aligned for a long
ensures that the long
is constructed into a buffer of sufficient size and with suitable alignment:
Code Block | ||||
---|---|---|---|---|
| ||||
#include <new> void f() { char c; // Used elsewhere in the function alignasstd::aligned_storage<sizeof(long) unsigned char buffer[sizeof, alignof(long)]>::type buffer; long *lp = ::new (&buffer) long; // ... } |
Noncompliant Code Example (Failure to Account For Array Overhead)
...