{"id":842,"date":"2010-07-23T01:06:11","date_gmt":"2010-07-23T07:06:11","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=842"},"modified":"2011-09-18T13:54:37","modified_gmt":"2011-09-18T05:54:37","slug":"%e5%86%85%e5%ad%98%e5%af%b9%e9%bd%90","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/842","title":{"rendered":"\u5b57\u8282\u5bf9\u9f50"},"content":{"rendered":"<p>\u4eca\u5929\u5728\u770b\u7ed3\u6784\u4f53\u548c\u5171\u7528\u4f53\u90e8\u5206\u7684\u65f6\u5019\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u65b0\u540d\u8bcd\u201c\u5185\u5b58\u5bf9\u9f50\u201d\u3002\u5148\u5f15\u5165\u95ee\u9898\u5427\u3002\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">struct student\r\n{\r\n\tchar name[20];\r\n\tint age;\r\n\tchar sex;\r\n\tchar phone[15];\r\n};\r\nstruct student p1;<\/pre>\n<p>sizeof(p1)=\uff1f<br \/>\n\u8fd9\u4e2a\u5f88\u7b80\u5355\u5f97\u51fa\u7b54\u6848\uff0c\u537320+4+1+15=40Byte\u3002\u5982\u679c\u5c06phone[15]\u6539\u4e3aphone[16]\uff0c\u7ed3\u679c\u662f44\u3002\u96be\u9053\u4e0d\u662f41\u5417\uff1f<\/p>\n<p>\u8fd9\u91cc\u4fbf\u8981\u5f15\u5165\u5185\u5b58\u5bf9\u9f50\u7684\u6982\u5ff5\u3002\u5185\u5b58\u4e3a\u4e86\u63d0\u9ad8\u8bbf\u95ee\u6548\u7387\uff0c\u4fbf\u89c4\u5b9a\u4ee5\u7ed3\u6784\u4f53\u4e2d\u6700\u5927\u7684\u57fa\u672c\u5355\u4f4d\u957f\u5ea6\u4e3a\u5bf9\u9f50\u6807\u51c6\u3002\u5373\u5b9e\u9645\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f\u662f\u5bf9\u9f50\u6807\u51c6\u7684\u6574\u6570\u500d\uff08\u5fc5\u8981\u6761\u4ef6\uff09\u3002\u5728\u4e0a\u9762\u7684\u7ed3\u6784\u4f53\u4e2d\uff0c\u6700\u5927\u7684\u57fa\u672c\u7c7b\u578b\u662fint\u3002\u56e0\u6b64\u4ee54Byte\u4e3a\u5bf9\u5176\u6807\u51c6\u3002\u6240\u4ee5\u5b9e\u9645\u5185\u5b58\u5927\u5c0f\u5e94\u8be5\u4e3a4\u7684\u6574\u6570\u500d\uff0c\u5373\u4e3a44Byte.<\/p>\n<p>\u4e5f\u8bb8\u4f60\u6709\u7591\u60d1\uff1aname[20]\u4e0d\u662f\u898120Byte\u5417\uff0c\u4e3a\u4ec0\u4e48\u4ee54Byte\u4e3a\u5bf9\u9f50\u6807\u51c6\uff1f\u8bf7\u6ce8\u610f\u8fd9\u91cc\u7684\u57fa\u672c\u7c7b\u578b\u3002name[20]\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\uff0c\u6570\u7ec4\u5c5e\u4e8e\u590d\u5408\u7684\u6570\u636e\u7c7b\u578b\u3002\u590d\u5408\u7684\u6570\u636e\u7c7b\u578b\u8fd8\u6709\u7ed3\u6784\u4f53\uff0c\u5171\u7528\u4f53\u3002\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\u662f\u6574\u578b\uff0c\u5b57\u7b26\u578b\uff0c\u6d6e\u70b9\u578b\u3002\u5982\u679c\u4f60\u8fd8\u6709\u4e0d\u89e3\uff0c\u90a3\u4e48\u770b\u4e0b\u9898\uff1a<\/p>\n<pre class=\"brush:c\">struct score\r\n{\r\n\tfloat english;\r\n\tfloat math;\r\n\tfloat computer\uff1b\r\n};\r\nstruct student\r\n{\r\n\tchar name[10];\r\n\tint age;\r\n\tchar sex;\r\n\tstruct score st_score;\r\n};<\/pre>\n<p>\u5728student\u7ed3\u6784\u4f53\u4e2d\u542b\u6709\u6570\u636e\u7c7b\u578b\u4e3astruct score\u8fd9\u6837\u7684\u53d8\u91cf\u3002struct score\u7684\u5927\u5c0f\u4e3a12Byte\uff0c\u4e5f\u662fstruct student\u7ed3\u6784\u4f53\u4e2d\u6700\u5927\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f46\u662f\u6211\u4eec\u7684\u5bf9\u9f50\u6807\u51c6\u662f4Byte\uff0c\u5c31\u5982\u6211\u4eec\u521a\u8bf4\u7684\u90a3\u6837\uff0c\u5185\u5b58\u7684\u5bf9\u9f50\u6807\u51c6\u662f\u53d6\u7ed3\u6784\u4f53\u4e2d\u6700\u5927\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u91cc\u6211\u4eec\u53d6sizeof(int)\u3002<\/p>\n<p>\u518d\u770b\u4e0b\u9762\u7684\u95ee\u9898\uff1a<\/p>\n<pre class=\"brush:c\">union data1\r\n{\r\n\tint i;\r\n\tchar c;\r\n\tchar str[9];\r\n};\r\nstruct data2\r\n{\r\n\tint i;\r\n\tchar c;\r\n\tchar str[9];\r\n};<\/pre>\n<p>sizeof(struct data1)=? sizeof(struct data2)=?<br \/>\n\u5bf9\u4e8e\u524d\u8005\uff0c\u7531\u4e8e\u5171\u7528\u4f53\u7684\u5b58\u50a8\u5927\u5c0f\u7531\u6700\u5927\u7684\u6210\u5458\u6765\u51b3\u5b9a\uff0c\u56e0\u6b64\u4e0a\u9898\u4e2d\u5171\u7528\u4f53\u7684\u5b58\u50a8\u5927\u5c0f\u4e3a9Byte\uff0c\u8003\u8651\u5230\u5185\u5b58\u5bf9\u9f50\uff0c\u5b83\u4ee5sizeof(int)=4Byte\u6765\u5bf9\u9f50\uff0c\u56e0\u6b64\u5b9e\u9645\u5185\u5b58\u5206\u914d\u5927\u5c0f\u662f12Byte\u3002\u5bf9\u4e8e\u540e\u8005\uff0c\u5b58\u50a8\u5927\u5c0f\u662f4+1+9=14Byte\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4ee54\u5bf9\u9f50\uff0c\u56e0\u6b64\u5b9e\u9645\u5206\u914d\u5927\u5c0f\u4e3a16Byte\u3002<\/p>\n<p>\u73b0\u5728\u4f60\u5e94\u8be5\u5bf9\u4e8e\u5171\u7528\u4f53\u548c\u7ed3\u6784\u4f53\u7684\u5185\u5b58\u5bf9\u9f50\u6709\u6240\u4e86\u89e3\u4e86\u3002\u90a3\u4e48\u518d\u63a5\u7740\u5f80\u4e0b\u770b\uff1a<\/p>\n<pre class=\"brush:c\">struct data\r\n{\r\n\tchar c;\r\n\tdouble d;\r\n\tchar ch;\r\n}<\/pre>\n<p>sizeof(struct data)=?<br \/>\n\u6709\u4e86\u4e0a\u9762\u7684\u7406\u89e3\uff0c10\u80af\u5b9a\u4e0d\u4f1a\u662f\u7b54\u6848\uff0c\u90a3\u4e48\u4f1a\u662f16\u5427\u3002\u4e5f\u9519\u3002\u6b63\u786e\u7b54\u6848\u662f24\u3002\u4f60\u53ef\u80fd\u4f1a\u611f\u5230\u83ab\u540d\u5176\u5999\uff1a\u6309\u7167\u4e0a\u9762\u6240\u8bf4\u7684\u65b9\u6cd5\uff0c\u4ee58\u4e3a\u5bf9\u9f50\u6807\u51c6\uff0c\u90a3\u4e4810\u4e0d\u662f8\u7684\u500d\u6570\uff0c\u90a3\u5c31\u662f16\u5440\u3002\u4e3a\u4ec0\u4e48\u5462\uff1f\u8bf7\u4f60\u8010\u5fc3\u770b\u4e0b\u9762\u3002<\/p>\n<p>\u9996\u5148\u6211\u4eec\u77e5\u9053\u7ed3\u6784\u4f53\u53d8\u91cf\u662f\u5206\u914d\u7684\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u3002d\u6beb\u65e0\u7591\u95ee\u662f\u5206\u914d8Byte\uff0c\u5bf9\u4e8ed\u201c\u524d\u9762\u201d\u7684c\uff0c\u6309\u7167\u5bf9\u9f50\u6807\u51c6\u6211\u4eec\u5e94\u8be5\u5206\u914d8Byte,\u800cd\u540e\u9762\u7684ch\uff0c\u6309\u7167\u5bf9\u9f50\u6807\u51c6\u4e5f\u5e94\u8be5\u5206\u914d8Byte\u3002\u56e0\u6b64\u7ed3\u679c\u662f24\u3002<\/p>\n<p>\u5982\u679c\u8fd9\u6837\uff1a<\/p>\n<pre class=\"brush:c\">struct data\r\n{\r\n\tdouble d;\r\n\tchar c;\r\n\tchar ch;\r\n}<\/pre>\n<p>sizeof(struct data)=16\u3002<\/p>\n<p>\u56e0\u4e3a\u5185\u5b58\u6ca1\u5fc5\u8981\u5206\u522b\u4e3ac\u548cch\u5206\u914d8Byte\u6765\u8fdb\u884c\u5bf9\u9f50\uff0c\u5c06\u5b83\u4eec\u653e\u5728\u4e00\u4e2a8byte\u91cc\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5185\u5b58\u5bf9\u9f50\u3002\u4e0a\u9762\u5728\u5f15\u5165\u5185\u5b58\u5bf9\u9f50\u6982\u5ff5\u7684\u65f6\u5019\uff0c\u6211\u7528\u62ec\u53f7\u7279\u522b\u6ce8\u91ca\u5fc5\u8981\u6761\u4ef6\u4fbf\u662f\u8bf4\u660e\u6b64\u5904\u5185\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5728\u770b\u7ed3\u6784\u4f53\u548c\u5171\u7528\u4f53\u90e8\u5206\u7684\u65f6\u5019\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u65b0\u540d\u8bcd\u201c\u5185\u5b58\u5bf9\u9f50\u201d\u3002\u5148\u5f15\u5165\u95ee\u9898\u5427\u3002\u5982\u4e0b\uff1a struct stude [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[109,122,124,123],"class_list":["post-842","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-122","tag-124","tag-123"],"views":8238,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/842","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/842\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=842"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}