{"id":3309,"date":"2017-06-02T22:04:24","date_gmt":"2017-06-02T14:04:24","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=3309"},"modified":"2017-06-02T22:04:24","modified_gmt":"2017-06-02T14:04:24","slug":"linux%e4%b8%8b%e7%94%a8c%e8%af%ad%e8%a8%80%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aa%e8%bf%94%e5%9b%9e%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/3309.html","title":{"rendered":"Linux\u4e0b\u7528C\u8bed\u8a00\u5b9e\u73b0#\u4e00\u4e2a\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u51fd\u6570"},"content":{"rendered":"<p>=Start=<\/p>\n<h4>\u7f18\u7531\uff1a<\/h4>\n<p>\u5728Linux\u4e0a\u9700\u8981\u7528C\u8bed\u8a00\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u53c2\u6570\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5bf9\u5e94\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u51fd\u6570\uff08\u6ce8\uff1a\u76f4\u63a5\u8fd4\u56de\u56fa\u5b9a\u5185\u5bb9\u7684\u5b57\u7b26\u4e32\u7684\u51fd\u6570\u4e0d\u5728\u6b64\u8ba8\u8bba\u8303\u56f4\u5185\uff09\u3002\u4f46\u662fC\u8bed\u8a00\u4e2d\u51fd\u6570\u5185\u7684\u53d8\u91cf\u662f\u6709\u751f\u547d\u5468\u671f\u7684\uff0c\u9700\u8981\u7528malloc\u52a8\u6001\u5206\u914d\u5185\u5b58\u4ee5\u9632\u6b62\u51fd\u6570\u8fd4\u56de\u65f6\u53d8\u91cf\u5931\u6548\uff1b\u53e6\u4e00\u4e2a\u65b9\u6cd5\u5c31\u662f\u76f4\u63a5\u5728\u88ab\u8c03\u7528\u5904\u7533\u8bf7\uff0c\u4e0d\u8fc7\u8fd9\u6837\u7684\u8bdd\u8c03\u7528\u8d77\u6765\u5c31\u6ca1\u90a3\u4e48\u65b9\u4fbf\uff1b\u8fd8\u6709\u4e00\u4e2a\u529e\u6cd5\u5c31\u662f\u5728\u51fd\u6570\u5185\u5bf9\u53d8\u91cf\u7528static\u5173\u952e\u5b57\u8fdb\u884c\u9650\u5b9a\uff0c\u4e0d\u8fc7\u8fd9\u6837\u5c31\u65e0\u6cd5\u7f16\u5199\u53ef\u91cd\u5165(re-entrant)\u7684\u51fd\u6570\u4e86\u3002<\/p>\n<p>\u597d\u4e45\u6ca1\u7528C\u8bed\u8a00\u5199\u8fd9\u7c7b\u7a0b\u5e8f\u4e86\uff0c\u4e0d\u592a\u597d\u628a\u63e1\uff0c\u6240\u4ee5\u53bb\u7f51\u4e0a\u641c\u7d22\u4e86\u4e00\u4e0b\uff0c\u628a\u770b\u5230\u7684\u6bd4\u8f83\u597d\u7684\u5185\u5bb9\u603b\u7ed3\u4e86\u4e00\u4e0b\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n<h4>\u6b63\u6587\uff1a<\/h4>\n<h5>\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u5728\u7f51\u4e0a\u641c\u7d22\u4e86\u4e00\u4e0b\uff0c\u5176\u5b9e\u6211\u8981\u7684\u8fd9\u4e2a\u51fd\u6570\u548c getwd() \u4ee5\u53ca get_current_dir_name() \u7684\u529f\u80fd\u5f88\u50cf\uff1a<\/p>\n<pre class=\"lang:default decode:true \">char *getwd(char *buf);\t\/\/ For portability and security reasons, use of getwd() is deprecated.\r\nchar *get_current_dir_name(void);\r\n\tget_current_dir_name() will malloc(3) an array big enough to hold the absolute pathname of the current working directory. If the environment variable PWD is set, and its value is correct, then that value will be returned. The caller should free(3) the returned buffer.<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>\u6211\u7684\u9700\u6c42\uff1a\u5b9e\u73b0\u4e00\u4e2a\u6839\u636e\u53c2\u6570\u8fd4\u56de\u5f53\u524d\u65f6\u95f4\u5bf9\u5e94\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u51fd\u6570\u3002<\/strong><\/span>\u8fd9\u6837\u7684\u529f\u80fd\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5efa\u8bae\u662f\u5728 function \u4e2d\u8c03\u7528 malloc \u52a8\u6001\u5206\u914d\u5185\u5b58\uff0c\u7136\u540e\u5728 caller \u90a3\u91cc\u8d1f\u8d23\u8fdb\u884c\u4f7f\u7528(call)\u548c\u91ca\u653e(free)\u3002<\/p>\n<pre class=\"lang:default decode:true \">\/* \u65b9\u6cd5\u4e00\uff1a\u8c03\u7528\u8005\u8d1f\u8d23\u5206\u914d\u5185\u5b58 *\/\r\n\/* OK: caller allocates the memory *\/\r\nchar buf[512];\r\nchar * my_func(char* ret) \/* called with \"buf\" *\/\r\n{\r\n  strcpy(ret, some_get_a_string_func()); \/* assign the string pointer *\/\r\n  return ret;\r\n}\r\n\r\n\/* \u65b9\u6cd5\u4e8c\uff1a\u5408\u6cd5\u4f46\u662f\u8be5\u51fd\u6570\u4e0d\u53ef\u91cd\u5165 *\/\r\n\/* Legal ... but not re-entrant.  \r\n   Every call to \"my_func()\" will step on every other call.\r\n   Definitely *NOT* OK with threads; probably not OK anywhere else  *\/\r\nchar * my_func()\r\n{\r\n  static char buf[512];\r\n  strcpy(buf, some_get_a_string_func()); \/* assign the string pointer *\/\r\n  return buf;\r\n}\r\n\r\n\/* \u65b9\u6cd5\u4e09\uff1a\u88ab\u8c03\u7528\u51fd\u6570\u5206\u914d\u5185\u5b58\uff0c\u8c03\u7528\u51fd\u6570\u9700\u8981\u8bb0\u5f97\u91ca\u653e\u5bf9\u5e94\u5185\u5b58 *\/\r\n\/* Also OK: callee allocates memory; caller (or SOMEBODY!) needs to remember to free it. *\/\r\nchar * my_func(int max_bytes)\r\n{\r\n  char *buf = malloc (max_bytes + 1);\r\n  if (buf)\r\n  {\r\n    \/* assign string pointer \r\n       protect against buffer overflow\r\n       insure return string is always NULL, or zero-terminated *\/\r\n    strncpy(buf, some_get_a_string_func(), max_bytes); \r\n    buf[max_bytes] = '\\0';\r\n  }\r\n  return buf ;\r\n}<\/pre>\n<h5>\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/22971847\/return-a-string-allocated-with-malloc\">https:\/\/stackoverflow.com\/questions\/22971847\/return-a-string-allocated-with-malloc<\/a>\u00a0#nice<\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/15454065\/returning-string-from-function-without-malloc\">https:\/\/stackoverflow.com\/questions\/15454065\/returning-string-from-function-without-malloc<\/a><\/li>\n<li><a href=\"https:\/\/linux.die.net\/man\/3\/get_current_dir_name\">https:\/\/linux.die.net\/man\/3\/get_current_dir_name<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/31060404\/how-can-i-return-a-character-array-from-a-function-in-c\">https:\/\/stackoverflow.com\/questions\/31060404\/how-can-i-return-a-character-array-from-a-function-in-c<\/a><\/li>\n<li><a href=\"http:\/\/www.linuxquestions.org\/questions\/programming-9\/return-char%2A-from-func-in-c-826209\/#post4066472\">http:\/\/www.linuxquestions.org\/questions\/programming-9\/return-char%2A-from-func-in-c-826209\/#post4066472<\/a>\u00a0#<span style=\"color: #ff0000;\"><strong>nice<\/strong><\/span><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5728Linux\u4e0a\u9700\u8981\u7528C\u8bed\u8a00\u5b9e\u73b0\u4e00\u4e2a\u51fd\u6570\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u53c2\u6570\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u5bf9\u5e94\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u51fd\u6570 [&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,295,30,797],"class_list":["post-3309","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-c","tag-free","tag-linux","tag-malloc"],"views":3654,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3309","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=3309"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3309\/revisions"}],"predecessor-version":[{"id":3325,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/3309\/revisions\/3325"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=3309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=3309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=3309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}