{"id":2850,"date":"2022-06-26T09:38:52","date_gmt":"2022-06-26T04:08:52","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=2850"},"modified":"2022-06-26T09:38:52","modified_gmt":"2022-06-26T04:08:52","slug":"c20-no_unique_address-attribute","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/","title":{"rendered":"C++20 [[no_unique_address]] Attribute"},"content":{"rendered":"<p>The <em><strong>[[no_unique_address]]<\/strong><\/em> attribute was introduced in <em><strong>C++20<\/strong><\/em> to give a compiler the freedom to optimise memory allocation of a struct\/class when it contains a subobject that does not have any members. The other requirement is that the subobject should not be a <em><strong>static<\/strong><\/em> member of the enclosing struct\/class.<\/p>\n<p>Let us start with the basics first. Consider the following example:<\/p>\n<figure id=\"attachment_2851\" aria-describedby=\"caption-attachment-2851\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"2851\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/code1-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1.jpg\" data-orig-size=\"913,647\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1656140668&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Empty vs. Non-empty Struct\" data-image-description=\"&lt;p&gt;Empty vs. Non-empty Struct&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Empty vs. Non-empty Struct&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1.jpg\" class=\"wp-image-2851\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1.jpg?resize=600%2C425&#038;ssl=1\" alt=\"Empty vs. Non-empty Struct\" width=\"600\" height=\"425\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1.jpg?resize=300%2C213&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1.jpg?resize=768%2C544&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1.jpg?w=913&amp;ssl=1 913w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2851\" class=\"wp-caption-text\"><strong>Empty vs. Non-empty Struct<\/strong><\/figcaption><\/figure>\n<p>In the example above, <em><strong>EmptyStruct<\/strong><\/em> does not have any members at all. So when an object of this type is defined, should the compiler allocate any memory for the object? The language says that it should. In other words, the <em><strong>sizeof(EmptyStruct)<\/strong><\/em>\u00a0must be at least 1 byte.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Let us look at <em><strong>Struct1<\/strong><\/em>. This is a normal struct with an integer member. In this case, we expect <em><strong>sizeof(Struct1)<\/strong><\/em>\u00a0to be equal to <em><strong>sizeof(int)<\/strong><\/em>. In general, due to padding issues, the size of the composite object could be greater than the sum of the individual objects.<\/p>\n<p>What about <em><strong>Struct2<\/strong><\/em>? Here, we have a member of type <em><strong>EmptyStruct<\/strong><\/em> in addition to an integer. So what should be the <em><strong>sizeof(Struct2)<\/strong><\/em>? Should it allocate any memory for the contained <em><strong>EmptyStruct<\/strong><\/em> member <em><strong>es<\/strong><\/em>? By default, the compiler will allocate memory for that member as well.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>When the above program is executed, here is the output:<\/p>\n<figure id=\"attachment_2852\" aria-describedby=\"caption-attachment-2852\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2852\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/output1-9\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output1.jpg\" data-orig-size=\"818,152\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1656139790&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output1.jpg\" class=\"wp-image-2852\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output1.jpg?resize=550%2C102&#038;ssl=1\" alt=\"Program Output\" width=\"550\" height=\"102\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output1.jpg?resize=300%2C56&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output1.jpg?resize=768%2C143&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output1.jpg?w=818&amp;ssl=1 818w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2852\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>It is worth noting that the compiler has allocated 8 bytes for object of type <em><strong>Struct2<\/strong><\/em>, while 4 bytes would have been sufficient (as in <em><strong>Struct1<\/strong><\/em>) theoretically.<\/p>\n<p>Here is where <em><strong>[[no_unique_address]]<\/strong><\/em>\u00a0attribute comes in to picture. We can <em><strong>annotate<\/strong><\/em> a member subobject of a class with this attribute, if we feel that there is no need for extra memory for that object (when it is empty) inside the enclosing object.<\/p>\n<p>A minor digression at this point. I am using <em><strong>Visual Studio 2022 Ver 17.2.3 (64 bit)<\/strong><\/em> running in <em><strong>Windows 10<\/strong><\/em>. In this version, Microsoft <a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/msvc-cpp20-and-the-std-cpp20-switch\/\" target=\"_blank\" rel=\"noopener\"><em><strong>recommends<\/strong><\/em><\/a>\u00a0using the qualified attribute <em><strong>[[msvc::no_unique_address]]<\/strong><\/em>\u00a0rather than the standard mandated <em><strong>[[no_unique_address]]<\/strong><\/em> attribute, for certain binary compatibility reasons (this will change in the future). So that is the attribute I have used in my code. For convenience, I am using this <em><strong>#define<\/strong><\/em>:<\/p>\n<p style=\"padding-left: 40px;\"><span style=\"color: #0000ff;\"><strong>#define NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]<\/strong><\/span><\/p>\n<p>OK, now take a look at the following code:<\/p>\n<figure id=\"attachment_2854\" aria-describedby=\"caption-attachment-2854\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"2854\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/code2-7\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code2.jpg\" data-orig-size=\"790,337\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1656140770&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Using no_unique_address Attribute\" data-image-description=\"&lt;p&gt;Using no_unique_address Attribute&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using no_unique_address Attribute&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code2.jpg\" class=\"wp-image-2854\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code2.jpg?resize=550%2C235&#038;ssl=1\" alt=\"Using no_unique_address Attribute\" width=\"550\" height=\"235\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code2.jpg?resize=300%2C128&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code2.jpg?resize=768%2C328&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code2.jpg?w=790&amp;ssl=1 790w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2854\" class=\"wp-caption-text\"><strong>Using no_unique_address Attribute<\/strong><\/figcaption><\/figure>\n<p>In this example, <em><strong>Struct3<\/strong><\/em> has the <em><strong>EmptyStruct es<\/strong><\/em> subobject marked as <em><strong>NO_UNIQUE_ADDRESS<\/strong><\/em>. Here is the output from the program:<\/p>\n<figure id=\"attachment_2855\" aria-describedby=\"caption-attachment-2855\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2855\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/output2-10\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output2.jpg\" data-orig-size=\"836,115\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1656140158&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Effect of no_unique_address\" data-image-description=\"&lt;p&gt;Effect of no_unique_address&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Effect of no_unique_address&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output2.jpg\" class=\"wp-image-2855\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output2.jpg?resize=550%2C76&#038;ssl=1\" alt=\"Effect of no_unique_address\" width=\"550\" height=\"76\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output2.jpg?resize=300%2C41&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output2.jpg?resize=768%2C106&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output2.jpg?w=836&amp;ssl=1 836w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2855\" class=\"wp-caption-text\"><strong>Effect of no_unique_address<\/strong><\/figcaption><\/figure>\n<p>As expected, the compiler has only allocated 4 bytes (size of int) for <em><strong>Struct3<\/strong><\/em> object. What is interesting is that the addresses of <em><strong>s3.i<\/strong><\/em> and <em><strong>s3.es<\/strong><\/em> are the same! This confirms that no extra memory has been reserved for <em><strong>s3.es<\/strong><\/em>.<\/p>\n<p>The next example introduces an additional twist.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<figure id=\"attachment_2856\" aria-describedby=\"caption-attachment-2856\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2856\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/code3-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code3.jpg\" data-orig-size=\"802,385\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1656140503&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Multiple Empty Subobjects\" data-image-description=\"&lt;p&gt;Multiple Empty Subobjects&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Multiple Empty Subobjects&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code3.jpg\" class=\"wp-image-2856\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code3.jpg?resize=550%2C264&#038;ssl=1\" alt=\"Multiple Empty Subobjects\" width=\"550\" height=\"264\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code3.jpg?resize=300%2C144&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code3.jpg?resize=768%2C369&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code3.jpg?resize=800%2C385&amp;ssl=1 800w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code3.jpg?w=802&amp;ssl=1 802w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2856\" class=\"wp-caption-text\"><strong>Multiple Empty Subobjects<\/strong><\/figcaption><\/figure>\n<p><em><strong>Struct4<\/strong><\/em> has two <em><strong>EmptyStruct<\/strong><\/em> members <em><strong>es<\/strong><\/em> and <em><strong>es2<\/strong><\/em>, and both are marked <em><strong>NO_UNIQUE_ADDRESS<\/strong><\/em>. What will be the size of a <em><strong>Struct4<\/strong><\/em> object and what will be the addresses of the three subobjects? Let us see the output:<\/p>\n<figure id=\"attachment_2857\" aria-describedby=\"caption-attachment-2857\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2857\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/output3-8\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output3.jpg\" data-orig-size=\"845,138\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1656140448&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output3.jpg\" class=\"wp-image-2857\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output3.jpg?resize=600%2C98&#038;ssl=1\" alt=\"Program Output\" width=\"600\" height=\"98\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output3.jpg?resize=300%2C49&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output3.jpg?resize=768%2C125&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output3.jpg?w=845&amp;ssl=1 845w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-2857\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>The size of the whole struct is just 4 bytes. It implies that the compiler has allocated space only for the <em><strong>int i<\/strong><\/em>\u00a0field. If you look at the addresses, it is clear that the memory space allocated for the <em><strong>int i<\/strong><\/em> object is shared with the two <em><strong>EmptyStruct<\/strong><\/em> objects, at the same time guaranteeing that the address of <em><strong>es<\/strong><\/em> is different from that of <em><strong>es2<\/strong><\/em>! Clearly, this memory optimisation is only because of <em><strong>NO_UNIQUE_ADDRESS<\/strong><\/em> attribute.<\/p>\n<p>One last example before we end this article. What happens if we annotate a non-empty subobject with <em><strong>NO_UNIQUE_ADDRESS<\/strong><\/em> attribute? The attribute will be obviosly ignored by the compiler.<span class=\"Apple-converted-space\">\u00a0<\/span><\/p>\n<p>Here is the example:<\/p>\n<figure id=\"attachment_2858\" aria-describedby=\"caption-attachment-2858\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2858\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/code4-2\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code4.jpg\" data-orig-size=\"965,466\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1656164895&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Non Empty Struct\" data-image-description=\"&lt;p&gt;Non Empty Struct&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Non Empty Struct&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code4.jpg\" class=\"wp-image-2858\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code4.jpg?resize=550%2C266&#038;ssl=1\" alt=\"Non Empty Struct\" width=\"550\" height=\"266\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code4.jpg?resize=300%2C145&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code4.jpg?resize=768%2C371&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code4.jpg?w=965&amp;ssl=1 965w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2858\" class=\"wp-caption-text\"><strong>Non Empty Struct<\/strong><\/figcaption><\/figure>\n<p>Here is the corresponding output:<\/p>\n<figure id=\"attachment_2859\" aria-describedby=\"caption-attachment-2859\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"2859\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/output4-4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output4.jpg\" data-orig-size=\"544,133\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1656164863&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"no_unique_address Ignored\" data-image-description=\"&lt;p&gt;no_unique_address Ignored&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;no_unique_address Ignored&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output4.jpg\" class=\"wp-image-2859\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output4.jpg?resize=550%2C134&#038;ssl=1\" alt=\"no_unique_address Ignored\" width=\"550\" height=\"134\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output4.jpg?resize=300%2C73&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/Output4.jpg?w=544&amp;ssl=1 544w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-2859\" class=\"wp-caption-text\"><strong>no_unique_address Ignored<\/strong><\/figcaption><\/figure>\n<p><span class=\"Apple-converted-space\">\u00a0<\/span>The output shows that extra space has been allocated for the <em><strong>NonEmptyStruct<\/strong><\/em> subobject and the addresses of the subobjects do not overlap.<\/p>\n<p>To recap what we have discussed so far, <em><strong>C++ 20<\/strong><\/em> allows us to use the <em><strong>[[no_unique_address]]<\/strong><\/em> attribute on <em><strong>non-static<\/strong> <\/em>and <em><strong>empty<\/strong><\/em> subobjects in a class\/struct to give a hint to the compiler to avoid allocating memory for the subobject. This is useful when targeting a resource-constrained environment such as an embedded controller.<\/p>\n<p>You can download the example source code <a href=\"http:\/\/www.rangakrish.com\/downloads\/NoUniqueAddress.cpp\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a great weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The [[no_unique_address]] attribute was introduced in C++20 to give a compiler the freedom to optimise memory allocation of a struct\/class when it contains a subobject that does not have any members. The other requirement is that the subobject should not be a static member of the enclosing struct\/class. Let us start with the basics first. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[49,17],"tags":[293,325],"class_list":["post-2850","post","type-post","status-publish","format-standard","hentry","category-c","category-programming","tag-c-20","tag-no_unique_address"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-JY","jetpack-related-posts":[{"id":2280,"url":"https:\/\/www.rangakrish.com\/index.php\/2021\/01\/17\/c20-nodiscard-attribute\/","url_meta":{"origin":2850,"position":0},"title":"C++20 [[nodiscard]] Attribute","author":"admin","date":"January 17, 2021","format":false,"excerpt":"[[nodiscard]]\u00a0attribute \u201cencourages\u201d the compiler to issue a warning when the return value from a function is ignored. It was introduced in C++ 17 and enhanced in C++ 20 to include a string literal that can be used as an explanation of the warning. Let us look at different cases one\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Case-1: Enumeration","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2021\/01\/Example1-src-300x261.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1905,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/04\/deleted-destructor-in-c\/","url_meta":{"origin":2850,"position":1},"title":"Deleted Destructor in C++","author":"admin","date":"March 4, 2020","format":false,"excerpt":"Since C++ 11, we can use the keyword delete to declare functions as deleted and thus prohibit the use of these functions in the code. See this article\u00a0for a brief overview. In particular, the destructor of a class\/struct\/union can be declared as deleted. In today\u2019s article, I am going to\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example-8","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example-8.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2024,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/06\/21\/stdis_standard_layout\/","url_meta":{"origin":2850,"position":2},"title":"std::is_standard_layout","author":"admin","date":"June 21, 2020","format":false,"excerpt":"In our last post, we learnt about the type trait std::is_trivial<T>. Today, let us go through another type trait that is quite similar. The expression\u00a0is_standard_layout<T>::value returns true if the layout of objects of type T is compiler independent, and hence is of standard format. Else, it returns false. This is\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"The Difference","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/06\/Example4.jpg?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":1946,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/03\/28\/stdis_empty\/","url_meta":{"origin":2850,"position":3},"title":"std::is_empty","author":"admin","date":"March 28, 2020","format":false,"excerpt":"In the previous post, we looked at the std::is_destructible<T> type trait. Today, let us try to understand another type trait std::is_empty<T>. As per the specification, is_empty<T>::value will return true in the following cases: - The class\/struct has no non-static data member - The class\/struct does not define a virtual function\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example4: Union and Bit Field","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/03\/Example4-1.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":666,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/10\/21\/c17-initalization-in-selection-statements\/","url_meta":{"origin":2850,"position":4},"title":"C++17: Initialization in Selection Statements","author":"admin","date":"October 21, 2017","format":false,"excerpt":"C++17 enhances if and switch statements with the ability to define\u00a0 variables whose life-time is limited to the corresponding scope. This is in keeping with the general guideline that variables should have a tight scope, i.e.,\u00a0 should be defined as close to the point of use as possible and should\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Sample Class","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/10\/classdefn.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2668,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/01\/23\/template-mixins-in-d-programming-language\/","url_meta":{"origin":2850,"position":5},"title":"Template Mixins in D Programming Language","author":"admin","date":"January 23, 2022","format":false,"excerpt":"When I heard the name Mixin\u00a0for the first time in the context of Dlang, I imagined it would be something similar to the mixins\u00a0of Common Lisp, but I was completely wrong!\u00a0 Mixins are a very interesting feature of D language. They allow code to be \"injected\" into the source at\u2026","rel":"","context":"In &quot;D Language&quot;","block_context":{"text":"D Language","link":"https:\/\/www.rangakrish.com\/index.php\/category\/d-language\/"},"img":{"alt_text":"Simple Template Mixin","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/01\/Defn-300x98.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=2850"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/2850\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=2850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=2850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=2850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}