{"id":16,"date":"2009-03-08T23:13:10","date_gmt":"2009-03-08T15:13:10","guid":{"rendered":"http:\/\/www.genepeng.com\/?p=16"},"modified":"2009-03-08T23:14:12","modified_gmt":"2009-03-08T15:14:12","slug":"sizeof","status":"publish","type":"post","link":"https:\/\/www.genepeng.com\/index.php\/16","title":{"rendered":"sizeof"},"content":{"rendered":"<p>c\u8bed\u8a00\u91cc\u7684sizeof \u662f\u4e00\u4e2a\u8ba1\u7b97\u6570\u636e\u5b58\u50a8\u7a7a\u95f4\u5927\u5c0f\u7684\u5355\u76ee\u8fd0\u7b97\u7b26\uff0c\u5b83\u8fd4\u56de\u6570\u636e\u6240\u5360\u7684\u5b57\u8282\u4e2a\u6570\uff0c\u3000\u6211\u4eec\u53ef\u4ee5\u5148\u6765\u770b\u770b\uff2d\uff33\uff2e\uff24\u91cc\u7684\u5b9a\u4e49\uff1a<\/p>\n<p><span style=\"color: #993300;\">sizeof Operator<br \/>\nsizeof expression<\/span><\/p>\n<p><span style=\"color: #993300;\">The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.<br \/>\nThe expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses).<\/span><\/p>\n<p><span style=\"color: #993300;\">When applied to a structure type or variable, sizeof returns the actual size, which may include padding bytes inserted for alignment.<br \/>\nWhen applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of<br \/>\ndynamically allocated arrays or external arrays<\/span><\/p>\n<p><span style=\"color: #000000;\">\u8fd9\u6bb5\u8bdd\u5927\u6982\u8bb2\u7684\u5c31\u662f\uff1a<\/span><\/p>\n<p><span style=\"color: #000000;\">\uff11\uff1asizeof\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5f97\u5230\u53d8\u91cf\u548c\u7c7b\u578b\u5b58\u50a8\u7a7a\u95f4\u7684\u5927\u5c0f\uff0c\u5927\u5c0f\u662f\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u7684\uff0c\u5982\u4e00\u4e2a\u53d8\u91cfa\u6240\u5360\u7684\u7a7a\u95f4\u662f\uff12\u4e2a\u5b57\u8282\uff0c\u90a3\u4e48sizeof(a) == 2\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\uff12\uff1a\u5982\u679ca\u662f\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\uff0c\u90a3\u4e48\u5fc5\u987b\u52a0\u62ec\u53f7\u8868\u793a\u4e3asizeof(a)\u5f62\u5f0f\uff0c\u5982\u679ca\u662f\u53d8\u91cfsizeof a\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u901a\u5e38\u90fd\u5199\u6210sizeof(a)\u5f62\u5f0f\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\uff13\uff1a\u5f53sizeof \u5e94\u7528\u4e8e\u7ed3\u6784\u4f53\u6216\u53d8\u91cf\u65f6\uff0c\u5b83\u8fd4\u56de\u5b9e\u9645\u6240\u5360\u7684\u7a7a\u95f4\u5927\u5c0f\uff0c\u8fd9\u4e2a\u7a7a\u95f4\u53ef\u80fd\u5305\u62ec\u7f16\u7ece\u5668\u4e3a\u4e86\u5185\u5b58\u5bf9\u9f50\u800c\u63d2\u5165\u7684\u989d\u5916\u7684\u5b57\u8282\u3002<\/span><\/p>\n<p><span style=\"color: #000000;\">\uff14\uff1a\u5f53sizeof\u5e94\u7528\u4e8e\u9759\u6001\u591a\u7ef4\u6570\u7ec4\u65f6\uff0c\u5b83\u8fd4\u56de\u6574\u4e2a\u6570\u636e\u7684\u5927\u5c0f\uff0c\u4f46sizeof <span style=\"color: #000000;\">\u4e0d\u80fd\u5e94\u7528\u4e8e\u5bf9\u6001\u5206\u914d\u7684\u6570\u7ec4\u3002<\/span><\/span><\/p>\n<p> \u4e0b\u9762\u4e3e\u4f8b\u8bf4\u660esizeof\u7684\u8fd0\u7528<\/p>\n<p>\uff11.\u3000\u7528\u4e8e\u7c7b\u578b<\/p>\n<p>sizeof(int) \/\/\u8fd4\u56de\uff14\uff0c\u8868\u793a\u6574\u5f62\u6570\u5360\uff14\u4e2a\u5b57\u8282\uff0c\u5176\u5b9e\u8fd9\u4e2a\u4e0e\u7cfb\u7edf\u6709\u5173\uff0c\u3000\u4e00\u822clinux\u548c32\u4f4dwindows\u90fd\u8fd4\u56de\uff14;<\/p>\n<p>sizeof(char) \/\/\u8fd4\u56de\uff11;<\/p>\n<p>struct student {<br \/>\n       int age;<br \/>\n       char name[20];<br \/>\n };<br \/>\nsizeof(student);\/\/ \u8fd4\u56de24;<\/p>\n<p>struct student {<br \/>\n       int age;<br \/>\n    char sex;<br \/>\n };<br \/>\nsizeof(student);\/\/ \u8fd4\u56de8, \u8fd9\u91cc\u4e3a\u4ec0\u4e48\u4f1a\u8fd4\u56de\uff18\u5462\uff0c\u4e00\u4e2aint\u5360\uff14\u4e2a\u5b57\u8282\uff0c\u3000\u4e00\u4e2achar\u5360\u4e00\u4e2a\u5b57\u8282\uff0c\u3000\u5e94\u8be5\u662f\uff15\u4e2a\u5b57\u8282\u554a\uff0c\u3000\u5176\u5b9e\u4e0d\u8fd9\u6837\u7684\uff0c\u3000\u7f16\u7ece\u5668\u4e3a\u4e86\u66f4\u9ad8\u6548\u7387\u800c\u4f7f\u5f97\u5185\u5b58\u5bf9\u9f50\uff0c\u3000\u4e3achar sex\u53e6\u5916\u63d2\u5165\u4e86\uff13\u4e2a\u5b57\u8282\uff0c\u3000\u6240\u4ee5\u8fd4\u56de\uff18\uff1b<\/p>\n<p>\uff12.\u3000\u7528\u4e8e\u53d8\u91cf<\/p>\n<p>int a = 100;<\/p>\n<p>sizeof(a);\/\/\u8fd4\u56de\uff14\uff0c\u3000\u4e00\u822c\u6839\u636e\u5b83\u7684\u7c7b\u578b\u800c\u51b3\u5b9a\u5b83\u6240\u5360\u7a7a\u95f4\u5927\u5c0f<\/p>\n<p>long float f = 33.00;<\/p>\n<p>sizeof(f );\/\/\u8fd4\u56de\uff18,<\/p>\n<p>char *p;<\/p>\n<p>sizeof(p );\/\/\u8fd4\u56de4, \u8fd9\u91ccp\u662f\u4e00\u4e2a\u6307\u9488\u53d8\u91cf\uff0csizeof(p)\u8fd4\u56de\u8fd9\u4e2a\u6307\u9488\u53d8\u91cf\u6240\u5360\u7684\u7a7a\u95f4\u5927\u5c0f, \u800c\u4e0d\u662f\u5b57\u7b26p\u6240\u5360\u7684\u7a7a\u95f4\u5927\u5c0f\uff08\u4e00\u4e2a\u5b57\u7b26\u53ea\u5360\u4e00\u4e2a\u5b57\u8282\uff09\u3002<\/p>\n<p>\uff13.\u3000\u7528\u4e8e\u6570\u7ec4<\/p>\n<p>int int_arr[5];<br \/>\nsizeof(int_arr);\/\/\u8fd4\u56de\uff12\uff10\uff0c\u3000\u800c\u4e0d\u662f\uff15\uff0c\u5f53\u7528\u4e8e\u6570\u7ec4\u65f6\uff0c\u3000\u5b83\u8fd4\u56de\u8fd9\u4e2a\u6570\u7ec4\u6240\u70b9\u7684\u603b\u5b57\u8282\u6570\uff0c\u3000\u800c\u4e0d\u662f\u6570\u7ec4\u65e0\u7d20\u7684\u4e2a\u6570;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>c\u8bed\u8a00\u91cc\u7684sizeof \u662f\u4e00\u4e2a\u8ba1\u7b97\u6570\u636e\u5b58\u50a8\u7a7a\u95f4\u5927\u5c0f\u7684\u5355\u76ee\u8fd0\u7b97\u7b26\uff0c\u5b83\u8fd4\u56de\u6570\u636e\u6240\u5360\u7684\u5b57\u8282\u4e2a\u6570\uff0c\u3000\u6211\u4eec\u53ef\u4ee5\u5148\u6765\u770b\u770b\uff2d\uff33\uff2e\uff24\u91cc\u7684\u5b9a\u4e49\uff1a sizeof Operator sizeof expression The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t. The expression is either an identifier or a type-cast expression (a type specifier enclosed in parentheses). When applied to a structure &#8230; <a title=\"sizeof\" class=\"read-more\" href=\"https:\/\/www.genepeng.com\/index.php\/16\" aria-label=\"More on sizeof\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[14,13],"_links":{"self":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":2,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":18,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions\/18"}],"wp:attachment":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}