{"id":45,"date":"2024-12-14T16:25:33","date_gmt":"2024-12-14T08:25:33","guid":{"rendered":"https:\/\/genetop.top\/?p=45"},"modified":"2024-12-14T16:27:10","modified_gmt":"2024-12-14T08:27:10","slug":"class-memory-layer","status":"publish","type":"post","link":"https:\/\/genetop.top\/index.php\/2024\/12\/14\/class-memory-layer\/","title":{"rendered":"class \u4ec0\u4e48\u65f6\u5019\u4f1a\u89e6\u53d1\u5185\u5b58\u5bf9\u9f50\uff1f"},"content":{"rendered":"<p>\u5728 C++ \u4e2d\uff0c<strong>\u7c7b\uff08<code>class<\/code>\uff09\u7684\u5185\u5b58\u5bf9\u9f50<\/strong>\u4f1a\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u89e6\u53d1\uff1a<\/p>\n<ol>\n<li><strong>\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u7c7b\u578b\u5177\u6709\u5bf9\u9f50\u8981\u6c42<\/strong>\u3002<\/li>\n<li><strong>\u7c7b\u4e2d\u5305\u542b\u7ee7\u627f\u5173\u7cfb\u65f6\uff0c\u57fa\u7c7b\u4e0e\u6d3e\u751f\u7c7b\u7684\u5bf9\u9f50\u53ef\u80fd\u53d7\u5f71\u54cd<\/strong>\u3002<\/li>\n<li><strong>\u5185\u5b58\u5bf9\u9f50\u53d7\u67b6\u6784\uff08\u5982 32 \u4f4d\u300164 \u4f4d\uff09\u548c\u7f16\u8bd1\u5668\u7684\u5b9e\u73b0\u7b56\u7565\u5f71\u54cd<\/strong>\u3002<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u8be6\u7ec6\u89e3\u6790 <strong>\u4ec0\u4e48\u65f6\u5019\u4f1a\u89e6\u53d1\u5185\u5b58\u5bf9\u9f50<\/strong> \u4ee5\u53ca <strong>\u5185\u5b58\u5bf9\u9f50\u7684\u89c4\u5219<\/strong>\u3002<\/p>\n<hr \/>\n<h2><strong>\u5185\u5b58\u5bf9\u9f50\u89e6\u53d1\u7684\u6761\u4ef6<\/strong><\/h2>\n<h3>1. <strong>\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u7c7b\u578b\u7684\u5bf9\u9f50\u8981\u6c42<\/strong><\/h3>\n<p>\u6bcf\u79cd\u6570\u636e\u7c7b\u578b\u90fd\u6709\u5176\u5bf9\u9f50\u8981\u6c42\uff08alignment requirements\uff09\uff0c\u7f16\u8bd1\u5668\u4f1a\u5c06\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u6309\u7167\u5176\u5bf9\u9f50\u8981\u6c42\u8fdb\u884c\u6392\u5217\uff0c\u5fc5\u8981\u65f6\u586b\u5145\u5b57\u8282\uff08padding\uff09\u4ee5\u6ee1\u8db3\u5bf9\u9f50\u89c4\u5219\u3002<br \/>\n<strong>\u5bf9\u9f50\u7684\u5173\u952e\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>\u6570\u636e\u7c7b\u578b\u7684\u5bf9\u9f50\u8981\u6c42\u662f\u7531\u5176\u5927\u5c0f\u51b3\u5b9a\u7684\uff0c\u4f8b\u5982\uff1a\n<ul>\n<li><code>char<\/code> \u901a\u5e38\u4e3a 1 \u5b57\u8282\u5bf9\u9f50\u3002<\/li>\n<li><code>int<\/code> \u901a\u5e38\u4e3a 4 \u5b57\u8282\u5bf9\u9f50\u3002<\/li>\n<li><code>double<\/code> \u901a\u5e38\u4e3a 8 \u5b57\u8282\u5bf9\u9f50\uff08\u5728 64 \u4f4d\u7cfb\u7edf\u4e0a\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u6210\u5458\u53d8\u91cf\u7684\u8d77\u59cb\u5730\u5740\u5fc5\u987b\u662f\u5176\u5bf9\u9f50\u8981\u6c42\u7684\u500d\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none\"><\/div>\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-cpp\"><code class=\"!whitespace-pre hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;cstddef&gt;<\/span><\/span><\/code><\/code><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Example<\/span> {<br \/>\n<span class=\"hljs-type\">char<\/span> a; <span class=\"hljs-comment\">\/\/ 1 \u5b57\u8282<\/span><br \/>\n<span class=\"hljs-type\">int<\/span> b; <span class=\"hljs-comment\">\/\/ 4 \u5b57\u8282<\/span><br \/>\n<span class=\"hljs-type\">char<\/span> c; <span class=\"hljs-comment\">\/\/ 1 \u5b57\u8282<\/span><br \/>\n};<\/p>\n<p><code class=\"!whitespace-pre hljs language-cpp\"><code class=\"!whitespace-pre hljs language-cpp\"><\/code><\/code><span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{<br \/>\nstd::cout &lt;&lt; <span class=\"hljs-string\">&#8220;Size of Example: &#8220;<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(Example) &lt;&lt; std::endl;<br \/>\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<p><strong>\u5206\u6790\uff1a<\/strong><\/p>\n<ol>\n<li><code>char a<\/code> \u5360 1 \u5b57\u8282\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\u7684 <code>int b<\/code> \u9700\u8981\u5bf9\u9f50\u5230 4 \u5b57\u8282\uff0c\u56e0\u6b64\u5728 <code>char a<\/code> \u540e\u63d2\u5165 3 \u5b57\u8282\u7684\u586b\u5145\u3002<\/li>\n<li><code>char c<\/code> \u5360 1 \u5b57\u8282\uff0c\u4f46\u7c7b\u7684\u5927\u5c0f\u6700\u7ec8\u9700\u8981\u6ee1\u8db3\u6700\u4e25\u683c\u7684\u6210\u5458\u5bf9\u9f50\uff08<code>int<\/code> \u7684\u5bf9\u9f50\u662f 4 \u5b57\u8282\uff09\uff0c\u56e0\u6b64\u6dfb\u52a0\u586b\u5145\u5230\u603b\u5927\u5c0f\u4e3a 12 \u5b57\u8282\u3002<\/li>\n<\/ol>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none\"><\/div>\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-yaml\"><span class=\"hljs-attr\">Size of Example:<\/span> <span class=\"hljs-number\">12<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<hr \/>\n<h3>2. <strong>\u57fa\u7c7b\u548c\u6d3e\u751f\u7c7b\u7684\u5bf9\u9f50<\/strong><\/h3>\n<p>\u5f53\u7c7b\u6709\u7ee7\u627f\u5173\u7cfb\u65f6\uff0c\u57fa\u7c7b\u7684\u5bf9\u9f50\u8981\u6c42\u4f1a\u5f71\u54cd\u6d3e\u751f\u7c7b\u7684\u5185\u5b58\u5e03\u5c40\uff0c\u901a\u5e38\u6d3e\u751f\u7c7b\u7684\u8d77\u59cb\u5730\u5740\u9700\u8981\u6ee1\u8db3\u57fa\u7c7b\u7684\u5bf9\u9f50\u8981\u6c42\u3002<\/p>\n<p><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none\"><\/div>\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-cpp\"><code class=\"!whitespace-pre hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;cstddef&gt;<\/span><\/span><\/code><\/code><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Base<\/span> {<br \/>\n<span class=\"hljs-type\">double<\/span> d; <span class=\"hljs-comment\">\/\/ 8 \u5b57\u8282\u5bf9\u9f50<\/span><br \/>\n};<\/p>\n<p><code class=\"!whitespace-pre hljs language-cpp\"><code class=\"!whitespace-pre hljs language-cpp\"><\/code><\/code><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Derived<\/span> : <span class=\"hljs-keyword\">public<\/span> Base {<br \/>\n<span class=\"hljs-type\">char<\/span> c; <span class=\"hljs-comment\">\/\/ 1 \u5b57\u8282<\/span><br \/>\n};<\/p>\n<p><code class=\"!whitespace-pre hljs language-cpp\"><code class=\"!whitespace-pre hljs language-cpp\"><\/code><\/code><span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{<br \/>\nstd::cout &lt;&lt; <span class=\"hljs-string\">&#8220;Size of Base: &#8220;<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(Base) &lt;&lt; std::endl;<br \/>\nstd::cout &lt;&lt; <span class=\"hljs-string\">&#8220;Size of Derived: &#8220;<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(Derived) &lt;&lt; std::endl;<br \/>\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<p><strong>\u5206\u6790\uff1a<\/strong><\/p>\n<ol>\n<li><code>Base<\/code> \u53ea\u6709\u4e00\u4e2a <code>double<\/code>\uff0c\u5360 8 \u5b57\u8282\u3002<\/li>\n<li><code>Derived<\/code> \u9700\u8981\u6ee1\u8db3 <code>Base<\/code> \u7684 8 \u5b57\u8282\u5bf9\u9f50\uff0c\u56e0\u6b64 <code>char c<\/code> \u540e\u4f1a\u586b\u5145 7 \u5b57\u8282\uff0c\u4fdd\u8bc1\u6574\u4e2a <code>Derived<\/code> \u7684\u5927\u5c0f\u662f <code>Base<\/code> \u7684\u500d\u6570\u3002<\/li>\n<\/ol>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none\"><\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-yaml\"><span class=\"hljs-attr\">Size of Base:<\/span> <span class=\"hljs-number\">8<\/span><br \/>\n<span class=\"hljs-attr\">Size of Derived:<\/span> <span class=\"hljs-number\">16<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<hr \/>\n<h3>3. <strong>\u865a\u51fd\u6570\u8868\u6307\u9488\uff08vptr\uff09\u7684\u5bf9\u9f50<\/strong><\/h3>\n<p>\u5982\u679c\u7c7b\u4e2d\u5305\u542b\u865a\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u6bcf\u4e2a\u5bf9\u8c61\u589e\u52a0\u4e00\u4e2a\u865a\u51fd\u6570\u8868\u6307\u9488\uff08<code>vptr<\/code>\uff09\uff0c\u8fd9\u4e2a\u6307\u9488\u901a\u5e38\u9075\u5faa\u6307\u9488\u7c7b\u578b\u7684\u5bf9\u9f50\u8981\u6c42\uff08\u901a\u5e38\u662f 4 \u5b57\u8282\u6216 8 \u5b57\u8282\uff09\u3002<\/p>\n<p><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none\"><\/div>\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\">\n<p><code class=\"!whitespace-pre hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;iostream&gt;<\/span><\/span><\/code><\/p>\n<p><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Base<\/span> {<br \/>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">func<\/span><span class=\"hljs-params\">()<\/span> <\/span>{}<br \/>\n};<\/p>\n<p><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">Derived<\/span> : <span class=\"hljs-keyword\">public<\/span> Base {<br \/>\n<span class=\"hljs-type\">char<\/span> c; <span class=\"hljs-comment\">\/\/ 1 \u5b57\u8282<\/span><br \/>\n};<\/p>\n<p><span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{<br \/>\nstd::cout &lt;&lt; <span class=\"hljs-string\">&#8220;Size of Base: &#8220;<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(Base) &lt;&lt; std::endl;<br \/>\nstd::cout &lt;&lt; <span class=\"hljs-string\">&#8220;Size of Derived: &#8220;<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(Derived) &lt;&lt; std::endl;<br \/>\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<p><strong>\u5206\u6790\uff1a<\/strong><\/p>\n<ol>\n<li><code>Base<\/code> \u4e2d\u6709\u865a\u51fd\u6570\uff0c\u56e0\u6b64\u5305\u542b\u4e00\u4e2a <code>vptr<\/code> \u6307\u9488\uff0864 \u4f4d\u7cfb\u7edf\u4e0a\u5360 8 \u5b57\u8282\uff09\u3002<\/li>\n<li><code>Derived<\/code> \u7ee7\u627f\u4e86\u865a\u51fd\u6570\u8868\u6307\u9488\uff0c\u540c\u65f6\u589e\u52a0\u4e86 <code>char c<\/code>\uff0c\u56e0\u6b64\u6700\u7ec8\u5927\u5c0f\u9700\u8981\u5bf9\u9f50\u5230 8 \u5b57\u8282\u3002<\/li>\n<\/ol>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-yaml\"><span class=\"hljs-attr\">Size of Base:<\/span> <span class=\"hljs-number\">8<\/span><br \/>\n<span class=\"hljs-attr\">Size of Derived:<\/span> <span class=\"hljs-number\">16<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<hr \/>\n<h3>4. <strong>\u5bf9\u9f50\u53d7\u7c7b\u5bf9\u9f50\u5c5e\u6027\uff08alignment specifier\uff09\u5f71\u54cd<\/strong><\/h3>\n<p>C++11 \u63d0\u4f9b\u4e86 <code>alignas<\/code> \u5173\u952e\u5b57\uff0c\u53ef\u4ee5\u663e\u5f0f\u8bbe\u7f6e\u7c7b\u6216\u53d8\u91cf\u7684\u5bf9\u9f50\u8981\u6c42\u3002<\/p>\n<p><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between rounded-t-md h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none\"><\/div>\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-cpp\"><code class=\"!whitespace-pre hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;cstddef&gt;<\/span><\/span><\/code><\/code><span class=\"hljs-keyword\">struct<\/span> <span class=\"hljs-title class_\">alignas<\/span>(<span class=\"hljs-number\">16<\/span>) Aligned {<br \/>\n<span class=\"hljs-type\">char<\/span> c;<br \/>\n};<\/p>\n<p><code class=\"!whitespace-pre hljs language-cpp\"><code class=\"!whitespace-pre hljs language-cpp\"><\/code><\/code><span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{<br \/>\nstd::cout &lt;&lt; <span class=\"hljs-string\">&#8220;Size of Aligned: &#8220;<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(Aligned) &lt;&lt; std::endl;<br \/>\nstd::cout &lt;&lt; <span class=\"hljs-string\">&#8220;Alignment of Aligned: &#8220;<\/span> &lt;&lt; <span class=\"hljs-built_in\">alignof<\/span>(Aligned) &lt;&lt; std::endl;<br \/>\n<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<p><strong>\u8f93\u51fa\u7ed3\u679c\uff1a<\/strong><\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-yaml\"><span class=\"hljs-attr\">Size of Aligned:<\/span> <span class=\"hljs-number\">16<\/span><br \/>\n<span class=\"hljs-attr\">Alignment of Aligned:<\/span> <span class=\"hljs-number\">16<\/span><br \/>\n<\/code><\/div>\n<\/div>\n<hr \/>\n<h3>5. <strong>\u7f16\u8bd1\u5668\u548c\u76ee\u6807\u5e73\u53f0\u5bf9\u9f50\u7b56\u7565<\/strong><\/h3>\n<ul>\n<li><strong>\u81ea\u7136\u5bf9\u9f50\uff08natural alignment\uff09\uff1a<\/strong><br \/>\n\u5927\u591a\u6570\u7f16\u8bd1\u5668\u4f1a\u9ed8\u8ba4\u5bf9\u9f50\u5230\u6570\u636e\u7c7b\u578b\u7684\u5927\u5c0f\u500d\u6570\uff08\u5982 <code>int<\/code> \u4e3a 4 \u5b57\u8282\u5bf9\u9f50\uff09\u3002<\/li>\n<li><strong>\u5f3a\u5236\u5bf9\u9f50\uff08forced alignment\uff09\uff1a<\/strong><br \/>\n\u67d0\u4e9b\u5e73\u53f0\u53ef\u80fd\u5f3a\u5236\u5bf9\u9f50\u5230\u66f4\u5927\u7684\u500d\u6570\uff08\u5982 64 \u4f4d\u7cfb\u7edf\u53ef\u80fd\u5bf9\u9f50\u5230 8 \u5b57\u8282\uff09\u3002<\/li>\n<li><strong>\u6570\u636e\u7ed3\u6784\u7684\u6574\u4f53\u5bf9\u9f50\uff1a<\/strong><br \/>\n\u7c7b\u7684\u5927\u5c0f\u4f1a\u5411\u6700\u4e25\u683c\u5bf9\u9f50\u8981\u6c42\u5bf9\u9f50\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2><strong>\u5982\u4f55\u67e5\u770b\u7c7b\u7684\u5185\u5b58\u5e03\u5c40<\/strong><\/h2>\n<ol>\n<li><strong>\u4f7f\u7528 <code>sizeof<\/code> \u67e5\u770b\u7c7b\u7684\u5927\u5c0f<\/strong>\uff1a\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-cpp\">std::cout &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(ClassName) &lt;&lt; std::endl;<br \/>\n<\/code><\/div>\n<\/div>\n<\/li>\n<li><strong>\u4f7f\u7528 <code>offsetof<\/code> \u67e5\u770b\u6210\u5458\u504f\u79fb<\/strong>\uff1a\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary dark:bg-gray-950\">\n<div class=\"sticky top-9 md:top-[5.75rem]\">\n<div class=\"absolute bottom-0 right-2 flex h-9 items-center\">\n<div class=\"flex items-center rounded bg-token-sidebar-surface-primary px-2 font-sans text-xs text-token-text-secondary dark:bg-token-main-surface-secondary\"><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre hljs language-cpp\">std::cout &lt;&lt; <span class=\"hljs-built_in\">offsetof<\/span>(ClassName, memberName) &lt;&lt; std::endl;<br \/>\n<\/code><\/div>\n<\/div>\n<\/li>\n<li><strong>\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\uff08\u5982 GDB\uff09\u67e5\u770b\u5e03\u5c40<\/strong>\u3002<\/li>\n<\/ol>\n<hr \/>\n<h3><strong>\u603b\u7ed3<\/strong><\/h3>\n<ul>\n<li><strong>\u7c7b\u7684\u5185\u5b58\u5bf9\u9f50\u4f1a\u5728\u4ee5\u4e0b\u573a\u666f\u89e6\u53d1\uff1a<\/strong>\n<ol>\n<li>\u6210\u5458\u53d8\u91cf\u7684\u7c7b\u578b\u6709\u5bf9\u9f50\u8981\u6c42\u3002<\/li>\n<li>\u7c7b\u4e2d\u5305\u542b\u7ee7\u627f\u5173\u7cfb\u3002<\/li>\n<li>\u7c7b\u6709\u865a\u51fd\u6570\u8868\u6307\u9488\u3002<\/li>\n<li>\u663e\u5f0f\u4f7f\u7528 <code>alignas<\/code> \u8bbe\u7f6e\u5bf9\u9f50\u3002<\/li>\n<\/ol>\n<\/li>\n<li><strong>\u5f71\u54cd\u5185\u5b58\u5bf9\u9f50\u7684\u56e0\u7d20\uff1a<\/strong>\n<ol>\n<li>\u6210\u5458\u53d8\u91cf\u7684\u7c7b\u578b\u3002<\/li>\n<li>\u7f16\u8bd1\u5668\u5b9e\u73b0\u548c\u76ee\u6807\u5e73\u53f0\u3002<\/li>\n<li>\u7c7b\u7684\u7ee7\u627f\u5173\u7cfb\u548c\u865a\u51fd\u6570\u3002<\/li>\n<\/ol>\n<\/li>\n<li><strong>\u4f18\u5316\u5efa\u8bae\uff1a<\/strong>\n<ul>\n<li>\u5c06\u5bf9\u9f50\u8981\u6c42\u8f83\u9ad8\u7684\u6210\u5458\u53d8\u91cf\u653e\u5728\u7c7b\u7684\u5f00\u5934\uff0c\u51cf\u5c11\u586b\u5145\u5b57\u8282\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C++ \u4e2d\uff0c\u7c7b\uff08class\uff09\u7684\u5185\u5b58\u5bf9\u9f50\u4f1a\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u89e6\u53d1\uff1a \u7c7b\u7684\u6210\u5458\u53d8\u91cf\u7c7b\u578b\u5177\u6709\u5bf9\u9f50\u8981\u6c42\u3002 \u7c7b\u4e2d\u5305\u542b\u7ee7\u627f\u5173 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-45","post","type-post","status-publish","format-standard","hentry","category-c","category-language"],"_links":{"self":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/45","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/comments?post=45"}],"version-history":[{"count":2,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions"}],"predecessor-version":[{"id":47,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/posts\/45\/revisions\/47"}],"wp:attachment":[{"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/media?parent=45"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/categories?post=45"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/genetop.top\/index.php\/wp-json\/wp\/v2\/tags?post=45"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}