{"id":329,"date":"2023-09-16T10:32:10","date_gmt":"2023-09-16T02:32:10","guid":{"rendered":""},"modified":"2023-09-18T11:50:03","modified_gmt":"2023-09-18T03:50:03","slug":"c%e8%af%ad%e8%a8%80%e6%95%b0%e7%bb%84%e5%88%9d%e5%a7%8b%e5%8c%96%e5%8f%8amalloc%e5%88%9d%e5%a7%8b%e5%8c%96","status":"publish","type":"post","link":"https:\/\/mushiming.com\/329.html","title":{"rendered":"C\u8bed\u8a00\u6570\u7ec4\u521d\u59cb\u5316\u53camalloc\u521d\u59cb\u5316"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u524d\u8a00<\/h2>\n

\u505a\u7b14\u8bd5\u9898\u7684\u65f6\u5019\u8001\u6709\u4e9b\u5c0f\u9519\u8bef\uff0c\u6765\u603b\u7ed3\u4e00\u4e0b\u3002<\/p>\n

\u6570\u7ec4\u8d4b\u503c\u521d\u59cb\u5316\u7684\u4e09\u4e2a\u65b9\u5f0f<\/h2>\n

1\u3001{0} \u521d\u59cb\u5316<\/h5>\n

\u4f8b\u5982\uff1a<\/p>\n

int<\/span> nums[<\/span>3<\/span>]<\/span> =<\/span> { \n   <\/span>0<\/span>}<\/span>;<\/span>\n<\/code><\/pre>\n
    \n
  • \u4f46\u662f\u7ecf\u5e38\u51fa\u73b0\u7684\u662f\u7ed9\u5b9a\u957f\u5ea6\uff08\u53d8\u91cf\uff09\u7136\u540e\u9700\u8981\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5982\u679c\u5199\u6210\u4e0b\u5f0f\uff1a<\/li>\n<\/ul>\n
    int<\/span> main<\/span>(<\/span>)<\/span>{ \n   <\/span>\n\tint<\/span> numsSize=<\/span>0<\/span>;<\/span>\n\tscanf<\/span>(<\/span>\"%d\"<\/span>,<\/span>&<\/span>numsSize)<\/span>;<\/span>\n\tint<\/span> nums[<\/span>numsSize]<\/span>=<\/span>{ \n   <\/span>0<\/span>}<\/span>;<\/span>\n\t}<\/span>\n<\/code><\/pre>\n
      \n
    • \u5219\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u62a5\u9519\u4fe1\u606f\uff1a<\/li>\n
    • error: variable-sized object may not be initialized?\u8fd9\u4e2a\u63d0\u793a\u662f\uff1a\u53d8\u91cf\u5927\u5c0f\u7684\u5bf9\u8c61\u4e0d\u80fd\u88ab\u521d\u59cb\u5316\u3002<\/li>\n
    • \u663e\u793a\u5982\u4e0b\u9519\u8bef\u4fe1\u606f\u7684\u539f\u56e0\u662f\u53ef\u4ee5\u7528\u53d8\u91cf\u5b9a\u4e49\u6570\u7ec4\u957f\u5ea6\uff0c\u4f46\u4e0d\u53ef\u521d\u59cb\u5316<\/strong>\u3002
      \"C\u8bed\u8a00\u6570\u7ec4\u521d\u59cb\u5316\u53camalloc\u521d\u59cb\u5316<\/li>\n<\/ul>\n
      2\u3001for \u5faa\u73af\u521d\u59cb\u5316<\/h5>\n
        \n
      • \u8be5\u65b9\u5f0f\u5e38\u7528\u4e8e\u83b7\u53d6\u8f93\u5165\u65f6\uff0c\u5c06for\u5faa\u73af\u4e2d\u7684\u5185\u5bb9\u6539\u4e3a**scanf(\"%d\",&nums[i]);**\u5373\u53ef\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n
        int<\/span> nums[<\/span>3<\/span>]<\/span>;<\/span>\nfor<\/span> (<\/span>int<\/span> i =<\/span> 0<\/span>;<\/span> i <<\/span> 3<\/span>;<\/span> i++<\/span>)<\/span> { \n   <\/span>\n   nums[<\/span>i]<\/span> =<\/span> i;<\/span>\n}<\/span>\n<\/code><\/pre>\n
        3\u3001memset \u51fd\u6570<\/h5>\n
          \n
        • memset \u4e00\u822c\u4f7f\u7528\u201c0\u201d\u521d\u59cb\u5316\u5185\u5b58\u5355\u5143\uff0c\u901a\u5e38\u662f\u7ed9\u6570\u7ec4\u6216\u7ed3\u6784\u4f53\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u6216\u6e05\u7a7a\u6570\u7ec4\u6216\u7ed3\u6784\u4f53\u3002<\/li>\n
        • \u56e0\u6b64\u53ef\u4ee5\u7528\u53d8\u91cf\u5b9a\u4e49\u6570\u7ec4\u957f\u5ea6\uff0c\u518d\u901a\u8fc7\u4f7f\u7528memset\u51fd\u6570\u4f7f\u6574\u4e2a\u6570\u7ec4\u7f6e0\u5b9e\u73b0\u521d\u59cb\u5316\u3002<\/li>\n
        • \u4f7f\u7528\u65f6\u9700\u8981\u4f7f\u7528\u5e93\uff0c#include <string.h><\/strong><\/li>\n<\/ul>\n
          int<\/span> main<\/span>(<\/span>)<\/span>{ \n   <\/span>\n\tint<\/span> numsSize=<\/span>0<\/span>;<\/span>\n\tscanf<\/span>(<\/span>\"%d\"<\/span>,<\/span>&<\/span>numsSize)<\/span>;<\/span>\n\tint<\/span> nums[<\/span>numsSize]<\/span>;<\/span>\n\tmemset<\/span>(<\/span>nums,<\/span>0<\/span>,<\/span>sizeof<\/span>(<\/span>int<\/span>)<\/span>*<\/span>numsSize)<\/span>;<\/span>\n\t}<\/span>\n<\/code><\/pre>\n

          \u6ce8\uff1a\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1amemset\u51fd\u6570\u6309\u5b57\u8282<\/strong>\u5bf9\u5185\u5b58\u5757\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u6240\u4ee5\u4e0d\u80fd\u7528\u5b83\u5c06 int \u6570\u7ec4\u521d\u59cb\u5316\u4e3a 0 \u548c -1 \u4e4b\u5916\u7684\u5176\u4ed6\u503c<\/strong>\u3002<\/p>\n

          \u4f7f\u7528malloc\u52a8\u6001\u5206\u914d\u5927\u5c0f<\/h2>\n
            \n
          • \u4f7f\u7528\u65f6\u9700\u5305\u542b\u5934\u6587\u4ef6\uff1a#include <stdlib.h><\/strong><\/li>\n<\/ul>\n
            1\u3001\u52a8\u6001\u7533\u8bf7\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h5>\n
            scanf<\/span>(<\/span>\"%d\"<\/span>,<\/span>&<\/span>numsSize)<\/span>;<\/span>\nint<\/span> *<\/span>temp =<\/span> (<\/span>int<\/span> *<\/span>)<\/span>malloc<\/span>(<\/span>sizeof<\/span>(<\/span>int<\/span>)<\/span> *<\/span> numsSize)<\/span>;<\/span> \n<\/code><\/pre>\n
            2\u3001\u52a8\u6001\u7533\u8bf7\u4e8c\u7ef4\u6570\u7ec4<\/h5>\n
              \n
            • \u9996\u5148\u901a\u8fc7\uff1apArray = (int **)malloc( n * sizeof(int * ) )\u8be5\u8bed\u53e5\u6765\u5206\u914d\u6240\u6709\u884c\u7684\u9996\u5730\u5740 \u3002\u63a5\u7740pArray[i] = (int *)malloc( m * sizeof(int) );\u6765\u5206\u914d\u6bcf\u884c\u7684\u9996\u5730\u5740\u3002<\/li>\n<\/ul>\n
              \t\/\/\u83b7\u53d6\u884c\u5217\u6570<\/span>\n\tscanf<\/span>(<\/span>\"%d %d\"<\/span>,<\/span> &<\/span>n,<\/span> &<\/span>m )<\/span>;<\/span>\n\t\/\/\u52a8\u6001\u5206\u914d\u5b58\u50a8\u6570\u7ec4\u7684\u5927\u5c0f<\/span>\n\tpArray =<\/span> (<\/span>int<\/span> *<\/span>*<\/span>)<\/span>malloc<\/span>(<\/span> n *<\/span> sizeof<\/span>(<\/span>int<\/span>*<\/span>)<\/span> )<\/span>;<\/span>\n\t\/\/\u4f7f\u7528\u53cc\u5c42\u5faa\u73af\u5c06\u6570\u636e\u8bfb\u5165\u6570\u7ec4\u4e2d<\/span>\n\tfor<\/span>(<\/span> i=<\/span>0<\/span>;<\/span> i<<\/span>n;<\/span> i++<\/span>)<\/span>\n\t{ \n   <\/span>\n\t      pArray[<\/span>i]<\/span> =<\/span> (<\/span>int<\/span>*<\/span>)<\/span>malloc<\/span>(<\/span> m *<\/span> sizeof<\/span>(<\/span>int<\/span>)<\/span> )<\/span>;<\/span>\n\t\tfor<\/span>(<\/span> j=<\/span>0<\/span>;<\/span> j<<\/span>m;<\/span> j++<\/span>)<\/span>\n\t\t{ \n   <\/span>\n\t\t\tscanf<\/span>(<\/span>\"%d\"<\/span>,<\/span> pArray[<\/span>i]<\/span> +<\/span> j )<\/span>;<\/span>\n\t\t}<\/span>\n\t}<\/span>\n<\/code><\/pre>\n
              3\u3001\u5bf9\u52a8\u6001\u6570\u7ec4\u8fdb\u884c\u521d\u59cb\u5316<\/h5>\n
                \n
              • \n

                \u5e0c\u671b\u5728\u5206\u914d\u5185\u5b58\u7684\u540c\u65f6\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u8bf7\u4f7f\u7528 calloc() \u51fd\u6570\u3002<\/p>\n<\/li>\n

              • \n

                calloc() \u51fd\u6570\u662f\u52a8\u6001\u7533\u8bf7\u5185\u5b58\u51fd\u6570\u4e4b\u4e00\uff0c\u76f8\u5f53\u4e8e\u7528malloc\u51fd\u6570\u7533\u8bf7\u5e76\u4e14\u521d\u59cb\u5316\u4e00\u6837\uff0ccalloc\u51fd\u6570\u4f1a\u5c06\u7533\u8bf7\u7684\u5185\u5b58\u5168\u90e8\u521d\u59cb\u5316\u4e3a0\u3002<\/p>\n<\/li>\n

              • \n

                calloc() \u51fd\u6570\u7528\u6765\u52a8\u6001\u5730\u5206\u914d\u5185\u5b58\u7a7a\u95f4\u5e76\u521d\u59cb\u5316\u4e3a 0\uff0c\u5176\u539f\u578b\u4e3a\uff1a
                void * calloc (size_t num, size_t size);<\/strong>
                calloc()\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570,\u5206\u522b\u4e3a\u5143\u7d20\u7684\u6570\u76ee<\/strong>\u548c\u6bcf\u4e2a\u5143\u7d20\u7684\u5927\u5c0f<\/strong>,\u8fd9\u4e24\u4e2a\u53c2\u6570\u7684\u4e58\u79ef\u5c31\u662f\u8981\u5206\u914d\u7684\u5185\u5b58\u7a7a\u95f4\u7684\u5927\u5c0f\u3002<\/p>\n<\/li>\n

              • \n

                calloc() \u5728\u5185\u5b58\u4e2d\u52a8\u6001\u5730\u5206\u914d num \u4e2a\u957f\u5ea6\u4e3a size<\/strong> \u7684\u8fde\u7eed\u7a7a\u95f4\uff0c\u5e76\u5c06\u6bcf\u4e00\u4e2a\u5b57\u8282\u90fd\u521d\u59cb\u5316\u4e3a 0\u3002\u6240\u4ee5\u5b83\u7684\u7ed3\u679c\u662f\u5206\u914d\u4e86
                num*size \u4e2a\u5b57\u8282\u957f\u5ea6\u7684<\/strong>\u5185\u5b58\u7a7a\u95f4\uff0c\u5e76\u4e14\u6bcf\u4e2a\u5b57\u8282\u7684\u503c\u90fd\u662f0\u3002
                \u5982\u679c size \u7684\u503c\u4e3a 0<\/strong>\uff0c\u90a3\u4e48\u8fd4\u56de\u503c\u4f1a\u56e0\u6807\u51c6\u5e93\u5b9e\u73b0\u7684\u4e0d\u540c\u800c\u4e0d\u540c\uff0c\u53ef\u80fd\u662f NULL\uff0c\u4e5f\u53ef\u80fd\u4e0d\u662f<\/strong>\uff0c\u4f46\u8fd4\u56de\u7684\u6307\u9488\u4e0d\u5e94\u8be5\u518d\u6b21\u88ab\u5f15\u7528\u3002<\/p>\n<\/li>\n<\/ul>\n

                 -<\/span>   int<\/span> *<\/span>res =<\/span> (<\/span>int<\/span>*<\/span>)<\/span>calloc<\/span>(<\/span>numsSize,<\/span> sizeof<\/span>(<\/span>int<\/span>)<\/span>)<\/span>;<\/span> \n -<\/span> \/\/\u65b9\u6cd5\u4e8c\uff1a<\/span>\n -<\/span>  int<\/span> *<\/span>res =<\/span> (<\/span>int<\/span>*<\/span>)<\/span>malloc<\/span>(<\/span>numsSize *<\/span> sizeof<\/span>(<\/span>int<\/span>)<\/span>)<\/span>;<\/span>\n    memset<\/span>(<\/span>res,<\/span> 0<\/span>,<\/span> numsSize *<\/span> sizeof<\/span>(<\/span>int<\/span>)<\/span>)<\/span>;<\/span>\n    \/\/\u6d4b\u8bd5\u4ee3\u7801<\/span>\n    for<\/span>(<\/span>int<\/span> i=<\/span>0<\/span>;<\/span>i<<\/span>numsSize;<\/span>i++<\/span>)<\/span>{ \n   <\/span>\n\t\tprintf<\/span>(<\/span>\"%d\\n\"<\/span>,<\/span>res[<\/span>i]<\/span>)<\/span>;<\/span>\n\t}<\/span>\n<\/code><\/pre>\n

                \u6ce8\uff1a\u7528memset\u7ed9\u6307\u9488\u53d8\u91cf\u5982p\u6240\u6307\u5411\u7684\u5185\u5b58\u5355\u5143\u8fdb\u884c\u521d\u59cb\u5316\u65f6\uff0cn \u5343\u4e07\u522b\u5199\u6210 sizeof( p )<\/strong>\uff0c\u56e0\u4e3a p \u662f\u6307\u9488\u53d8\u91cf\uff0c\u4e0d\u7ba1 p \u6307\u5411\u4ec0\u4e48\u7c7b\u578b\u7684\u53d8\u91cf\uff0csizeof( p ) \u7684\u503c\u90fd\u662f 4\u3002<\/p>\n

                \u53c2\u8003\u6587\u732e<\/h2>\n
                \n

                C\u8bed\u8a00\u6570\u7ec4\u521d\u59cb\u5316\u7684\u4e09\u79cd\u65b9\u5f0f
                \u6570\u7ec4\u7684\u5b9a\u4e49\uff0c\u521d\u59cb\u5316\u548c\u4f7f\u7528\uff0cC\u8bed\u8a00\u6570\u7ec4\u8be6\u89e3
                C\u8bed\u8a00-\u6570\u7ec4\u7684\u521d\u59cb\u5316\uff0c\u6570\u7ec4\u548c\u6307\u9488\uff0c\u51fd\u6570\u4e0e\u6570\u7ec4
                C\u8bed\u8a00\u4e2d\u7684malloc\u4f7f\u7528\u8be6\u89e3
                C\u8bed\u8a00malloc\u521d\u59cb\u5316\u95ee\u9898
                C\u8bed\u8a00\u4e2d\u7684malloc\u3001new\u3001memset\u51fd\u6570\u89e3\u6790
                C\u8bed\u8a00 \u7ed3\u6784\u4f53\u4f7f\u7528malloc\u52a8\u6001\u7533\u8bf7\u5185\u5b58\u65f6\uff0c\u5173\u4e8e\u5982\u4f55\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u7684\u89e3\u51b3\u65b9\u6cd5
                \u6570\u7ec4\u76f8\u5173\u77e5\u8bc6\u5b66\u4e60\u603b\u7ed3\uff08\u53camalloc\u4e0ememset\u521d\u59cb\u5316\u95ee\u9898\uff09\uff08C\u8bed\u8a00\uff09<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"C\u8bed\u8a00\u6570\u7ec4\u521d\u59cb\u5316\u53camalloc\u521d\u59cb\u5316\u524d\u8a00\u505a\u7b14\u8bd5\u9898\u7684\u65f6\u5019\u8001\u6709\u4e9b\u5c0f\u9519\u8bef\uff0c\u6765\u603b\u7ed3\u4e00\u4e0b\u3002\u6570\u7ec4\u8d4b\u503c\u521d\u59cb\u5316\u7684\u4e09\u4e2a\u65b9\u5f0f1\u3001{0}\u521d\u59cb\u5316\u4f8b\u5982\uff1ain...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/329"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=329"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/329\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}