{"id":3341,"date":"2017-06-06T21:16:04","date_gmt":"2017-06-06T13:16:04","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3341"},"modified":"2017-06-06T21:16:04","modified_gmt":"2017-06-06T13:16:04","slug":"malloc%e7%9a%84%e7%9f%a5%e8%af%86%e7%82%b9%e5%ad%a6%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3341.html","title":{"rendered":"malloc\u7684\u77e5\u8bc6\u70b9\u5b66\u4e60"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u5468\u672b\u95f2\u6765\u65e0\u4e8b\uff0c\u6df1\u5165\u5b66\u4e60\u3001\u6574\u7406\u4e00\u4e0bmalloc\/calloc\u7684\u77e5\u8bc6<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<h6><a href=\"https:\/\/stackoverflow.com\/questions\/8029584\/why-does-malloc-initialize-the-values-to-0-in-gcc\">Why does malloc initialize the values to 0 in gcc?<\/a><\/h6>\n<p><b>\u7b80\u800c\u8a00\u4e4b\uff1a<\/b><\/p>\n<p>\u6ca1\u6709\uff0c\u53ea\u4e0d\u8fc7\u5728\u4f60\u7684\u4f8b\u5b50\u4e2d\u5b83\u6070\u597d\u662f\u96f6\u3002<\/p>\n<p>\u5e76\u4e14\uff0c\u4f60\u7684\u6d4b\u8bd5\u7528\u4f8b\u5e76\u6ca1\u6709\u663e\u793a(\u6240\u6709\u7684)\u6570\u636e\u90fd\u4e3a0\u3002\u5b83\u53ea\u663e\u793a\u4e86\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u4e3a0\u3002<\/p>\n<hr \/>\n<p><b>\u590d\u6742\u4e00\u4e9b\u7684\u89e3\u91ca\uff1a<\/b><\/p>\n<p><span style=\"color: #ff0000;\">\u5f53\u4f60\u8c03\u7528malloc()\u65f6\uff0c\u6709\u4e24\u4ef6\u4e8b\u4f1a\u53d1\u751f<\/span>\uff1a<\/p>\n<ol>\n<li>\u5b83\u4ece\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e4b\u524d\u5206\u914d\u548c\u91ca\u653e\u7684\u5185\u5b58\u4e2d\u8fdb\u884c\u56de\u6536\u540e\u518d\u5206\u914d\uff1b<\/li>\n<li>\u5b83\u5411\u64cd\u4f5c\u7cfb\u7edf\u7533\u8bf7\u65b0\u7684\u5185\u5b58\u9875\uff1b<\/li>\n<\/ol>\n<p>\u5728\u7b2c\u4e00\u79cd\u60c5\u51b5\u4e0b\uff0c\u7533\u8bf7\u7684\u5185\u5b58\u4e2d\u5c06\u5305\u542b\u4ee5\u524d\u5206\u914d\u4e2d\u5269\u4f59\u7684\u6570\u636e\u3002\u6240\u4ee5\u5b83\u4e0d\u4f1a\u662f\u96f6\u3002\u8fd9\u662f\u6267\u884c\u5c0f\u5757\u5185\u5b58\u5206\u914d\u65f6\u7684\u5e38\u89c1\u60c5\u5f62\u3002<\/p>\n<p>\u5728\u7b2c\u4e8c\u79cd\u60c5\u51b5\u4e0b\uff0c\u7533\u8bf7\u7684\u5185\u5b58\u6765\u81ea\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u3002\u5f53\u7a0b\u5e8f\u8017\u5c3d\u5185\u5b58\u65f6\uff0c\u6216\u8005\u5f53\u4f60\u8bf7\u6c42\u4e00\u4e2a\u975e\u5e38\u5927\u5757\u5185\u5b58\u7684\u5206\u914d\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u3002(\u5728\u4f60\u7684\u4f8b\u5b50\u4e2d\u4e5f\u662f\u5982\u6b64)<\/p>\n<p>\u8fd9\u91cc\u5c31\u6709\u4e00\u4e2a\u95ee\u9898\uff1a<span style=\"color: #ff0000;\"><strong>\u51fa\u4e8e\u5b89\u5168\u8003\u8651\uff0c\u6765\u81ea\u64cd\u4f5c\u7cfb\u7edf\u7684\u5185\u5b58\u5c06\u88ab\u5f52\u96f6<\/strong><\/span>\u3002<\/p>\n<p>\u5f53\u64cd\u4f5c\u7cfb\u7edf\u7ed9\u4f60\u5206\u914d\u5185\u5b58\u65f6\uff0c\u5185\u5b58\u53ef\u80fd\u662f\u4ece\u4e0d\u540c\u7684\u8fdb\u7a0b\u4e2d\u91ca\u653e\u51fa\u6765\u7684\u3002\u56e0\u6b64\uff0c\u5185\u5b58\u5728\u53ef\u80fd\u4f1a\u5305\u542b\u654f\u611f\u4fe1\u606f\uff0c\u6bd4\u5982\u5bc6\u7801\u3002\u4e3a\u4e86\u9632\u6b62\u4f60\u8bfb\u53d6\u8fd9\u4e9b\u6570\u636e\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5728\u5b83\u7ed9\u4f60\u4e4b\u524d\u5c06\u5b83\u5f52\u96f6\u3002<\/p>\n<p><sub>*\u6211\u6ce8\u610f\u5230\uff0cC\u6807\u51c6\u5728\u5e76\u6ca1\u6709\u8bf4\u660e\u8fd9\u4e00\u70b9\u3002\u8fd9\u662f\u4e25\u683c\u7684\u64cd\u4f5c\u7cfb\u7edf\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u8fd9\u4e2a\u5f52\u96f6\u64cd\u4f5c\u53ef\u80fd\u4e0d\u5b58\u5728\u4e8e\u5b89\u5168\u4e0d\u53d7\u5173\u6ce8\u7684\u7cfb\u7edf\u4e2d\u3002<\/sub><\/p>\n<hr \/>\n<p>\u4e3a\u4e86\u63d0\u4f9b\u66f4\u591a\u548c\u6027\u80fd\u76f8\u5173\u7684\u80cc\u666f\u77e5\u8bc6\uff1a<\/p>\n<p>\u6b63\u5982 @R. \u5728\u8bc4\u8bba\u4e2d\u63d0\u5230\u7684\uff0c\u8fd9\u4e2a\u5f52\u96f6\u64cd\u4f5c\u5c31\u662f\u4e3a\u4ec0\u4e48\u4f60\u5e94\u8be5\u603b\u662f\u4f7f\u7528calloc()\u800c\u4e0d\u662fmalloc()+memset()\u7ec4\u5408\u3002\u56e0\u4e3acalloc()\u53ef\u4ee5\u5229\u7528\u8fd9\u4e2a\u4e8b\u5b9e\u6765\u907f\u514d\u5355\u72ec\u7684memset()\u3002<\/p>\n<hr \/>\n<p>\u4e0d\u8fc7\u53e6\u4e00\u65b9\u9762\uff0c\u8fd9\u79cd\u5f52\u96f6\u64cd\u4f5c\u6709\u65f6\u4f1a\u662f\u6027\u80fd\u74f6\u9888\u3002\u5728\u4e00\u4e9b\u6570\u503c\u5e94\u7528\u7a0b\u5e8f\u4e2d(\u6bd4\u5982\u4e0d\u5b58\u5728\u7684FFT)\uff0c\u4f60\u9700\u8981\u5206\u914d\u5927\u91cf\u7684scratch\u5185\u5b58\u3002\u4f7f\u7528\u5b83\u6267\u884c\u4efb\u4f55\u7b97\u6cd5\uff0c\u7136\u540e\u91ca\u653e\u5b83\u3002<\/p>\n<p>\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5f52\u96f6\u64cd\u4f5c\u662f\u4e0d\u5fc5\u8981\u7684\uff0c\u5e76\u4e14\u662f\u7eaf\u7cb9\u7684\u5f00\u9500\u3002<\/p>\n<p>\u6211\u6240\u89c1\u8fc7\u7684\u6700\u6781\u7aef\u7684\u4f8b\u5b50\u662f\uff0c\u572870\u79d2\u7684\u64cd\u4f5c\u4e2d\u82b1\u8d39\u4e8620\u79d2\u7684\u65f6\u95f4\u5bf948 GB\u7684\u7f13\u51b2\u533a\u8fdb\u884c\u5f52\u96f6\u64cd\u4f5c\u3002(\u5927\u7ea630%\u7684\u5f00\u9500)(\u5f53\u7136\uff0c\u8fd9\u53f0\u673a\u5668\u786e\u5b9e\u7f3a\u5c11\u5185\u5b58\u5e26\u5bbd\u3002)<\/p>\n<p>\u6700\u660e\u663e\u7684\u89e3\u51b3\u65b9\u6848\u662f\u7b80\u5355\u5730\u624b\u52a8\u91cd\u7528\u5185\u5b58\u3002\u4f46\u8fd9\u901a\u5e38\u9700\u8981\u6253\u7834\u5df2\u7ecf\u5efa\u7acb\u7684\u63a5\u53e3\u3002(\u7279\u522b\u662f\u5982\u679c\u5b83\u662f\u5e93\u4f8b\u7a0b\u7684\u4e00\u90e8\u5206)<\/p>\n<hr \/>\n<blockquote>\n<div id=\"modeWrapper\" class=\"read-mode show-reading-mode\"><span style=\"color: #ff0000;\"><strong>malloc()<\/strong><\/span> allocates size bytes and returns a pointer to the allocated memory.<br \/>\n<span style=\"color: #ff0000;\">The memory is not cleared.<\/span> If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().<\/div>\n<\/blockquote>\n<hr \/>\n<h6><a href=\"https:\/\/stackoverflow.com\/questions\/1538420\/difference-between-malloc-and-calloc\">Difference between malloc and calloc?<\/a>\uff08malloc\u548ccalloc\u7684\u4e0d\u540c\u4e4b\u5904\uff09<\/h6>\n<pre class=\"lang:default decode:true \">\/\/ void *malloc(size_t size);\r\nptr = (char **) malloc (MAXELEMS * sizeof(char *));\r\n\r\n\/\/ void *calloc(size_t nmemb, size_t size);\r\nptr = (char **) calloc (MAXELEMS, sizeof(char*));<\/pre>\n<p>calloc()\u4f1a\u7528\u96f6\u521d\u59cb\u5316\u7f13\u51b2\u533a\uff0c\u800cmalloc()\u5219\u672a\u5c06\u5185\u5b58\u521d\u59cb\u5316\u3002<\/p>\n<p class=\"tgt\">\u5f52\u96f6\u64cd\u4f5c\u53ef\u80fd\u9700\u8981\u82b1\u8d39\u4e00\u4e9b\u65f6\u95f4\uff0c\u56e0\u6b64\u5982\u679c\u6027\u80fd\u662f\u4e00\u4e2a\u95ee\u9898\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u9700\u8981\u4f7f\u7528malloc()\u3002\u5982\u679c\u521d\u59cb\u5316\u5185\u5b58\u66f4\u91cd\u8981\uff0c\u8bf7\u4f7f\u7528calloc()\u3002\u6bd4\u5982\uff0c\u7528\u4e86calloc()\u4f60\u5c31\u4e0d\u9700\u8981\u5728malloc()\u4e4b\u5916\u518d\u8c03\u7528memset()\u3002<\/p>\n<hr \/>\n<h6><a href=\"https:\/\/stackoverflow.com\/questions\/911860\/does-malloc-lazily-create-the-backing-pages-for-an-allocation-on-linux-and-othe\">Does malloc lazily create the backing pages for an allocation on Linux (and other platforms)?<\/a><\/h6>\n<p class=\"tgt\">Linux\u786e\u5b9e\u5ef6\u8fdf\u4e86\u9875\u9762(\u5185\u5b58)\u5206\u914d\uff0c\u4e5f\u5c31\u662f\u300c\u4e50\u89c2\u7684\u5185\u5b58\u5206\u914d\u300d\u3002\u4ecemalloc\u8fd4\u56de\u7684\u5185\u5b58\u6ca1\u6709\u4efb\u4f55\u4e1c\u897f\u652f\u6301\uff0c\u5f53\u4f60\u4f7f\u7528\u5b83\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u78b0\u5230\u4e00\u4e2aOOM\u72b6\u51b5(\u5982\u679c\u4f60\u8bf7\u6c42\u7684\u9875\u9762\u6ca1\u6709\u4ea4\u6362\u7a7a\u95f4)\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<a href=\"http:\/\/linux-mm.org\/OOM_Killer\">\u8fdb\u7a0b\u4f1a\u88ab\u968f\u610f\u7ec8\u6b62<\/a>\u3002<\/p>\n<p class=\"tgt\">\u53ef\u4ee5\u53bb\u00a0<a href=\"http:\/\/www.linuxdevcenter.com\/pub\/a\/linux\/2006\/11\/30\/linux-out-of-memory.html\">http:\/\/www.linuxdevcenter.com\/pub\/a\/linux\/2006\/11\/30\/linux-out-of-memory.html<\/a>\u00a0\u770b\u5b9e\u9645\u4f8b\u5b50\u3002<\/p>\n<hr \/>\n<h6 class=\"tgt\">\u4ec0\u4e48\u65f6\u5019\u8be5\u4f7f\u7528malloc\uff1f\uff08<a href=\"https:\/\/stackoverflow.com\/questions\/1788655\/when-to-use-malloc-for-char-pointers\">When to use malloc for char pointers<\/a>\uff09<\/h6>\n<p class=\"tgt\">\u5f53\u4f60\u9700\u8981 <span style=\"color: #ff0000;\"><strong>\u521b\u5efa<\/strong><strong>\/\u683c\u5f0f\u5316 \u4e00\u4e2a\u65b0\u5b57\u7b26\u4e32<\/strong><\/span>\u65f6\uff0c\u5c31\u5e94\u8be5\u4f7f\u7528malloc\uff08\u7136\u540e\u8981\u8bb0\u5f97free\uff09\uff1b\u5982\u679c\u53ea\u662f\u7b80\u5355\u7684\u6307\u5411\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u4e0d\u9700\u8981\u7528malloc\u5728\u5b57\u7b26\u4e32\u6307\u9488\u4e0a\u3002<\/p>\n<hr \/>\n<h6 class=\"tgt\">\u662f\u5426\u6709\u5fc5\u8981\u5728malloc()\u4e4b\u540e\u663e\u793a\u8c03\u7528memset()\u5bf9\u7533\u8bf7\u7684\u5185\u5b58\u8fdb\u884c\u521d\u59cb\u5316\u64cd\u4f5c\uff1f\uff08<a href=\"https:\/\/stackoverflow.com\/questions\/13410550\/do-i-have-to-memset-after-malloc-new-memory\">Do I have to memset after malloc new memory?<\/a>\uff09<\/h6>\n<div class=\"row\">\n<div id=\"outputText\" class=\"row small_font\">\n<div class=\"translated_result\">\n<p class=\"tgt\">malloc\u4e0d\u4f1a\u5bf9\u7531\u5b83\u5206\u914d\u7684\u5185\u5b58\u8fdb\u884c\u521d\u59cb\u5316\u64cd\u4f5c\u3002\u6240\u4ee5\uff0c\u4f60\u5f97\u5230\u7684\u5185\u5bb9\u91cc\u9762\u7684\u6570\u636e\u662f\u968f\u673a\u7684\u3002\u5982\u679c\u4f60\u771f\u7684\u9700\u8981\u5c06\u4e00\u5207\u8bbe\u7f6e\u4e3a0\uff0c\u90a3\u4e48\u5c31\u4f7f\u7528\u5728\u6027\u80fd\u4e0a\u6709\u4e00\u5b9a\u635f\u8017\u7684calloc\u6765\u5b9e\u73b0\u3002(\u5982\u679c\u4f60\u9700\u8981\u521d\u59cb\u5316\u4e3a\u975e0\u7684\u5185\u5bb9\uff0c\u90a3\u4e48\u53ef\u4ee5<span style=\"color: #ff0000;\">\u4f7f\u7528memset\u5bf9<strong>\u5b57\u8282\u6570\u7ec4<\/strong>\u8fdb\u884c\u521d\u59cb\u5316<\/span>\uff0c<span style=\"color: #0000ff;\"><strong>\u5176\u5b83\u7c7b\u578b\u7684\u5c31\u9700\u8981\u624b\u52a8\u5bf9\u6570\u7ec4\u8fdb\u884c\u5faa\u73af\u6765\u521d\u59cb\u5316\u5b83<\/strong><\/span>\u3002)<\/p>\n<hr \/>\n<h6 class=\"tgt\">\u5bf9\u5185\u5b58\u8fdb\u884c\u5f52\u96f6\u64cd\u4f5c\uff08<a href=\"https:\/\/stackoverflow.com\/questions\/2884230\/zeroing-out-memory\">zeroing out memory<\/a>\uff09<\/h6>\n<pre class=\"lang:default decode:true\">\/\/ first choice\r\nchar buffer[1024] = {0};\r\n\r\n\/\/ second choice\r\nchar buffer[1024];\r\nmemset(buffer, 0, sizeof(buffer));\r\n\r\n\/\/ second.2 choice\r\nchar *buffer;\r\nbuffer = malloc(sizeof(char) * 1024 );\r\nif( buffer == NULL ) {\r\n    \/* Error management *\/\r\n}\r\nmemset(buffer, 0, sizeof(char)*1024 );\r\n\r\n\/\/ third choice\r\nchar *buffer;\r\nbuffer = calloc(sizeof(char), 1024 );\r\nif( buffer == NULL ) {\r\n    \/* Error management *\/\r\n}<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li>\u5982\u4e0a<\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5468\u672b\u95f2\u6765\u65e0\u4e8b\uff0c\u6df1\u5165\u5b66\u4e60\u3001\u6574\u7406\u4e00\u4e0bmalloc\/calloc\u7684\u77e5\u8bc6 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[100,808,30,797],"class_list":["post-3341","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-c","tag-calloc","tag-linux","tag-malloc"],"views":4419,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=3341"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3341\/revisions"}],"predecessor-version":[{"id":3342,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3341\/revisions\/3342"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}