|
| 1 | +--- |
| 2 | +title: "/Zc:alignedNew (C++17 over-aligned allocation) | Microsoft Docs" |
| 3 | +ms.date: "12/14/2017" |
| 4 | +ms.technology: ["cpp-tools"] |
| 5 | +ms.topic: "article" |
| 6 | +f1_keywords: ["/Zc:alignedNew"] |
| 7 | +dev_langs: ["C++"] |
| 8 | +helpviewer_keywords: ["/Zc:alignedNew", "Zc:alignedNew", "-Zc:alignedNew"] |
| 9 | +author: "corob-msft" |
| 10 | +ms.author: "corob" |
| 11 | +manager: "ghogen" |
| 12 | +--- |
| 13 | +# /Zc:alignedNew (C++17 over-aligned allocation) |
| 14 | + |
| 15 | +Enable support for C++17 over-aligned **new**, dynamic memory allocation aligned on boundaries greater than the default for the maximum-sized standard aligned type, **max\_align\_t**. |
| 16 | + |
| 17 | +## Syntax |
| 18 | + |
| 19 | +> **/Zc:alignedNew**[-] |
| 20 | +
|
| 21 | +## Remarks |
| 22 | + |
| 23 | +Visual Studio version 15.5 enables compiler and library support for C++17 standard over-aligned dynamic memory allocation. When the **/Zc:alignedNew** option is specified, a dynamic allocation such as `new Example;` respects the alignment of *Example* even when it’s greater than `max_align_t`, the largest alignment required for any fundamental type. When the alignment of the allocated type is no more than that guaranteed by the original operator **new**, available as the value of the predefined macro **\_\_STDCPP\_DEFAULT\_NEW\_ALIGNMENT\_\_**, the statement `new Example;` results in a call to `::operator new(size_t)` as it did in C++14. When the alignment is greater than **\_\_STDCPP\_DEFAULT\_NEW\_ALIGNMENT\_\_**, the implementation instead obtains the memory by using `::operator new(size_t, align_val_t)`. Similarly, deletion of over-aligned types invokes `::operator delete(void*, align_val_t)` or the sized delete signature `::operator delete(void*, size_t, align_val_t)`. |
| 24 | + |
| 25 | +The **/Zc:alignedNew** option is only available when [/std:c++17](std-specify-language-standard-version.md) or [/std:c++latest](std-specify-language-standard-version.md) is enabled. Under **/std:c++17** or **/std:c++latest**, **/Zc:alignedNew** is enabled by default to conform to the ISO C++17 standard. If the only reason you implement operator **new** and **delete** is to support over-aligned allocations, you may no longer need this code in C++17 mode. To turn this option off and revert to the C++14 behavior of **new** and **delete** when **/std::c++17** or **/std:c++latest** is specified, specify **/Zc:alignedNew-**. If you implement operator **new** and **delete** but you are not ready to implement the over-aligned operator **new** and **delete** overloads that have the `align_val_t` parameter, use the **/Zc:alignedNew-** option to prevent the compiler and Standard Library from generating calls to the over-aligned overloads. |
| 26 | + |
| 27 | +## Example |
| 28 | + |
| 29 | +This sample shows how operator **new** and operator **delete** behave when the **/Zc:alignedNew** option is set. |
| 30 | + |
| 31 | +```cpp |
| 32 | +// alignedNew.cpp |
| 33 | +// Compile by using: cl /EHsc /std:c++17 /W4 alignedNew.cpp |
| 34 | +#include <iostream> |
| 35 | +#include <malloc.h> |
| 36 | +#include <new> |
| 37 | + |
| 38 | +// "old" unaligned overloads |
| 39 | +void* operator new(std::size_t size) { |
| 40 | + auto ptr = malloc(size); |
| 41 | + std::cout << "unaligned new(" << size << ") = " << ptr << '\n'; |
| 42 | + return ptr ? ptr : throw std::bad_alloc{}; |
| 43 | +} |
| 44 | + |
| 45 | +void operator delete(void* ptr, std::size_t size) { |
| 46 | + std::cout << "unaligned sized delete(" << ptr << ", " << size << ")\n"; |
| 47 | + free(ptr); |
| 48 | +} |
| 49 | + |
| 50 | +void operator delete(void* ptr) { |
| 51 | + std::cout << "unaligned unsized delete(" << ptr << ")\n"; |
| 52 | + free(ptr); |
| 53 | +} |
| 54 | + |
| 55 | +// "new" over-aligned overloads |
| 56 | +void* operator new(std::size_t size, std::align_val_t align) { |
| 57 | + auto ptr = _aligned_malloc(size, static_cast<std::size_t>(align)); |
| 58 | + std::cout << "aligned new(" << size << ", " << |
| 59 | + static_cast<std::size_t>(align) << ") = " << ptr << '\n'; |
| 60 | + return ptr ? ptr : throw std::bad_alloc{}; |
| 61 | +} |
| 62 | + |
| 63 | +void operator delete(void* ptr, std::size_t size, std::align_val_t align) { |
| 64 | + std::cout << "aligned sized delete(" << ptr << ", " << size << |
| 65 | + ", " << static_cast<std::size_t>(align) << ")\n"; |
| 66 | + _aligned_free(ptr); |
| 67 | +} |
| 68 | + |
| 69 | +void operator delete(void* ptr, std::align_val_t align) { |
| 70 | + std::cout << "aligned unsized delete(" << ptr << |
| 71 | + ", " << static_cast<std::size_t>(align) << ")\n"; |
| 72 | + _aligned_free(ptr); |
| 73 | +} |
| 74 | + |
| 75 | +struct alignas(256) OverAligned {}; // warning C4324, structure is padded |
| 76 | + |
| 77 | +int main() { |
| 78 | + delete new int; |
| 79 | + delete new OverAligned; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +This output is typical for 32-bit builds. The pointer values vary based on where your application runs in memory. |
| 84 | + |
| 85 | +```Output |
| 86 | +unaligned new(4) = 009FD0D0 |
| 87 | +unaligned sized delete(009FD0D0, 4) |
| 88 | +aligned new(256, 256) = 009FE800 |
| 89 | +aligned sized delete(009FE800, 256, 256) |
| 90 | +``` |
| 91 | + |
| 92 | +For information about conformance issues in Visual C++, see [Nonstandard Behavior](../../cpp/nonstandard-behavior.md). |
| 93 | + |
| 94 | +### To set this compiler option in the Visual Studio development environment |
| 95 | + |
| 96 | +1. Open the project's **Property Pages** dialog box. For details, see [Working with Project Properties](../../ide/working-with-project-properties.md). |
| 97 | + |
| 98 | +1. Select the **Command Line** property page in the **C/C++** folder. |
| 99 | + |
| 100 | +1. Modify the **Additional Options** property to include **/Zc:alignedNew** or **/Zc:alignedNew-** and then choose **OK**. |
| 101 | + |
| 102 | +## See also |
| 103 | + |
| 104 | +[/Zc (Conformance)](../../build/reference/zc-conformance.md) |
0 commit comments