{"id":404,"date":"2023-02-25T10:49:43","date_gmt":"2023-02-25T02:49:43","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=404"},"modified":"2023-04-29T21:37:02","modified_gmt":"2023-04-29T13:37:02","slug":"android-ndk-basic-c-dynamic-memory-allocation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-ndk-basic-c-dynamic-memory-allocation\/","title":{"rendered":"Android NDK\u57fa\u78404\uff1aC_\u52a8\u6001\u5185\u5b58\u5206\u914d"},"content":{"rendered":"<h2>\u52a8\u6001\u5206\u914d\u7c7b\u578b<\/h2>\n<pre><code class=\"language-c\">void main() {\n    \/\/stack overflow\u9519\u8bef\uff0c\u6808\u6ea2\u51fa\n    \/\/\u9759\u6001\u5185\u5b58\u5206\u914d\n    int a[1024 * 1024 * 10]; \/\/40M\n    \/\/\u6808\u5185\u5b58\n\n    \/\/C\u8bed\u8a00\u5185\u5b58\u5206\u914d\uff1a\n    \/\/1.\u6808\u533a(stack)\n    \/\/windows\u4e0b\uff0c\u6808\u5185\u5b58\u5206\u914d2M\uff08\u786e\u5b9a\u7684\u5e38\u6570\uff09\uff0c\u8d85\u51fa\u9650\u5236\uff0c\u63d0\u793astack overflow\u9519\u8bef\n    \/\/\u81ea\u52a8\u5206\u914d\uff0c\u81ea\u52a8\u91ca\u653e\n    \/\/2.\u5806\u533a(heap)\n    \/\/\u7a0b\u5e8f\u5458\u624b\u52a8\u5206\u914d\u91ca\u653e\uff0c\u64cd\u4f5c\u7cfb\u7edf80%\u5185\u5b58\n    \/\/3.\u5168\u5c40\u533a\u6216\u9759\u6001\u533a\n    \/\/4.\u5b57\u7b26\u5e38\u91cf\u533a\n    \/\/5.\u7a0b\u5e8f\u4ee3\u7801\u533a\n\n    getchar();\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u6808\u5185\u5b58\u4e0e\u5806\u5185\u5b58<\/h2>\n<pre><code class=\"language-c\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;Windows.h&gt;\n\n\/\/\u6808\u5185\u5b58\nvoid stackFun() {\n    int a[1024];\n    \/\/\u6808\u5185\u5b58\u81ea\u52a8\u91ca\u653e\n}\n\n\/\/\u5806\u5185\u5b58\nvoid heapFun() {\n    \/\/40M\u5185\u5b58\n    \/\/void *\u4efb\u610f\u7c7b\u578b\u7684\u6307\u9488\n    int* p = malloc(1024 * 1024 * 10 * sizeof(int)); \/\/\u5b57\u8282\u4e3a\u5355\u4f4d\n\n    \/\/\u91ca\u653e\n    free(p);\n}\n\nvoid main() {    \n    \/\/\u5728\u5806\u5185\u5b58\u4e0a\uff0c\u5206\u914d40M\u7684\u5185\u5b58\n    while (1) {\n        Sleep(1000);\n        \/\/heapFun();\n        stackFun();\n    }\n\n    getchar();\n}<\/code><\/pre>\n<h2>\u52a8\u6001\u5185\u5b58\u5206\u914d<\/h2>\n<pre><code class=\"language-c\">#define _CRT_SECURE_NO_WARNINGS\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;Windows.h&gt;\n\n\/\/\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u52a8\u6001\u6307\u5b9a\u6570\u7ec4\u7684\u5927\u5c0f\n\/\/\uff08\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u968f\u610f\u7684\u5f00\u8f9f\u6307\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\uff0c\u4ee5\u4f9b\u4f7f\u7528\uff0c\u76f8\u5f53\u4e8eJava\u4e2d\u7684\u96c6\u5408\uff09\n\/\/\u9759\u6001\u5185\u5b58\u5206\u914d\uff0c\u5206\u914d\u5185\u5b58\u5927\u5c0f\u7684\u662f\u56fa\u5b9a\uff0c\u95ee\u9898\uff1a1.\u5f88\u5bb9\u6613\u8d85\u51fa\u6808\u5185\u5b58\u7684\u6700\u5927\u503c 2.\u4e3a\u4e86\u9632\u6b62\u5185\u5b58\u4e0d\u591f\u7528\u4f1a\u5f00\u8f9f\u66f4\u591a\u7684\u5185\u5b58\uff0c\u5bb9\u6613\u6d6a\u8d39\u5185\u5b58\n\/\/\u52a8\u6001\u5185\u5b58\u5206\u914d\uff0c\u5728\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\uff0c\u52a8\u6001\u6307\u5b9a\u9700\u8981\u4f7f\u7528\u7684\u5185\u5b58\u5927\u5c0f\uff0c\u624b\u52a8\u91ca\u653e\uff0c\u91ca\u653e\u4e4b\u540e\u8fd9\u4e9b\u5185\u5b58\u8fd8\u53ef\u4ee5\u88ab\u91cd\u65b0\u4f7f\u7528\uff08\u6d3b\u6c34\uff09\nvoid main() {\n    \/\/\u9759\u6001\u5185\u5b58\u5206\u914d\u521b\u5efa\u6570\u7ec4\uff0c\u6570\u7ec4\u7684\u5927\u5c0f\u662f\u56fa\u5b9a\u7684\n    \/\/int i = 10;\n    \/\/int a[i]; \/\/\u4e0d\u80fd\u4f7f\u7528\u53d8\u91cf\u4f5c\u4e3a\u6570\u7ec4\u58f0\u660e\u5927\u5c0f\n\n    int len;\n    printf(&quot;\u8f93\u5165\u6570\u7ec4\u7684\u957f\u5ea6\uff1a&quot;);\n    scanf(&quot;%d&quot;, &amp;len);\n\n    \/\/\u5f00\u8f9f\u5185\u5b58\uff0c\u5927\u5c0flen*4\u5b57\u8282\n    int* p = malloc(len * sizeof(int));\n    \/\/p\u662f\u6570\u7ec4\u7684\u9996\u5730\u5740\uff0cp\u5c31\u662f\u6570\u7ec4\u7684\u540d\u79f0\n    \/\/\u7ed9\u6570\u7ec4\u5143\u7d20\u8d4b\u503c\uff08\u4f7f\u7528\u8fd9\u4e00\u5757\u521a\u521a\u5f00\u8f9f\u51fa\u6765\u7684\u5185\u5b58\u533a\u57df\uff09\n    int i = 0;\n    for (; i &lt; len - 1; i++) {\n        p[i] = rand() % 100;\n        printf(&quot;%d,%#x\\n&quot;, p[i], &amp;p[i]);\n    }\n\n    \/\/\u624b\u52a8\u91ca\u653e\u5185\u5b58\n    free(p);\n\n    getchar();\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/realloc \u91cd\u65b0\u5206\u914d\u5185\u5b58\nvoid main() {\n    int len;\n    printf(&quot;\u7b2c\u4e00\u6b21\u8f93\u5165\u6570\u7ec4\u7684\u957f\u5ea6\uff1a&quot;);\n    scanf(&quot;%d&quot;, &amp;len);\n\n    \/\/int* p = malloc(len * sizeof(int));    \n    int* p = calloc(len, sizeof(int)); \/\/\u6ce8\u610fcalloc\u4e0emalloc\u7684\u53c2\u6570\u533a\u522b\n    int i = 0;\n    for (; i &lt; len; i++) {\n        p[i] = rand() % 100;\n        printf(&quot;%d,%#x\\n&quot;, p[i], &amp;p[i]);\n    }\n\n    int addLen;\n    printf(&quot;\u8f93\u5165\u6570\u7ec4\u589e\u52a0\u7684\u957f\u5ea6\uff1a&quot;);\n    scanf(&quot;%d&quot;, &amp;addLen);\n    \/\/\u5185\u5b58\u4e0d\u591f\u7528\uff0c\u6269\u5927\u521a\u521a\u5206\u914d\u7684\u5185\u5b58\u7a7a\u95f4\n    \/\/realloc\u53c2\u6570\uff1a1.\u539f\u6765\u5185\u5b58\u7684\u6307\u9488 2.\u5185\u5b58\u6269\u5927\u4e4b\u540e\u7684\u603b\u5927\u5c0f        \n    int* p2 = realloc(p, sizeof(int) * (len + addLen));\n    if (p2 == NULL) {\n        printf(&quot;\u91cd\u65b0\u5206\u914d\u5931\u8d25&quot;);\n    }\n    \/\/\u91cd\u65b0\u5206\u914d\u5185\u5b58\u7684\u4e24\u79cd\u60c5\u51b5\uff1a\n    \/\/\u7f29\u5c0f\uff0c\u7f29\u5c0f\u7684\u90a3\u4e00\u90e8\u5206\u6570\u636e\u4f1a\u4e22\u5931\n    \/\/\u6269\u5927\uff0c\uff08\u8fde\u7eed\u7684\uff09\n    \/\/1.\u5982\u679c\u5f53\u524d\u5185\u5b58\u6bb5\u540e\u9762\u6709\u9700\u8981\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u76f4\u63a5\u6269\u5c55\u8fd9\u6bb5\u5185\u5b58\u7a7a\u95f4\uff0crealloc\u8fd4\u56de\u539f\u6307\u9488\n    \/\/2.\u5982\u679c\u5f53\u524d\u5185\u5b58\u6bb5\u540e\u9762\u7684\u7a7a\u95f2\u5b57\u8282\u4e0d\u591f\uff0c\u90a3\u4e48\u5c31\u4f7f\u7528\u5806\u4e2d\u7684\u7b2c\u4e00\u4e2a\u80fd\u591f\u6ee1\u8db3\u8fd9\u4e00\u8981\u6c42\u7684\u5185\u5b58\u5757\uff0c\u5c06\u76ee\u524d\u7684\u6570\u636e\u590d\u5236\u5230\u65b0\u7684\u4f4d\u7f6e\uff0c\u5e76\u5c06\u539f\u6765\u7684\u6570\u636e\u5e93\u91ca\u653e\u6389\uff0c\u8fd4\u56de\u65b0\u7684\u5185\u5b58\u5730\u5740\n    \/\/3.\u5982\u679c\u7533\u8bf7\u5931\u8d25\uff0c\u8fd4\u56deNULL\uff0c\u539f\u6765\u7684\u6307\u9488\u4ecd\u7136\u6709\u6548\n\n    \/\/\u91cd\u65b0\u8d4b\u503c\n    i = 0;\n    printf(&quot;--------------------------\\n&quot;);\n    for (; i &lt; len + addLen; i++) {\n        p2[i] = rand() % 200;\n        printf(&quot;%d,%#x\\n&quot;, p2[i], &amp;p2[i]);\n    }\n\n    \/\/\u624b\u52a8\u91ca\u653e\u5185\u5b58\n    \/*\n    \/\/p\u4e0ep2\u76f8\u7b49\u6216\u5df2\u7ecf\u81ea\u52a8\u91ca\u653e\uff0c\u65e0\u9700\u624b\u52a8\u91ca\u653e\n    if (p != NULL) {\n        free(p);\n        p = NULL;\n    }\n    *\/\n    if (p2 != NULL) {\n        free(p2);\n        p2 = NULL;\n    }\n\n    getchar();\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u5185\u5b58\u5206\u914d\u7684\u51e0\u4e2a\u6ce8\u610f\u7ec6\u8282\n\/\/1.\u4e0d\u80fd\u591a\u6b21\u91ca\u653e\n\/\/2.\u91ca\u653e\u5b8c\u4e4b\u540e\uff08\u6307\u9488\u4ecd\u7136\u6709\u503c\uff09\uff0c\u7ed9\u6307\u9488\u7f6eNULL\uff0c\u6807\u5fd7\u91ca\u653e\u5b8c\u6210\n\/\/3.\u5185\u5b58\u6cc4\u9732\uff08p\u91cd\u65b0\u8d4b\u503c\u4e4b\u540e\uff0c\u518dfree\uff0c\u5e76\u6ca1\u6709\u771f\u6b63\u91ca\u653e\u5185\u5b58\uff09\nvoid main() {\n    int len;\n    printf(&quot;\u8f93\u5165\u6570\u7ec4\u7684\u957f\u5ea6\uff1a&quot;);\n    scanf(&quot;%d&quot;, &amp;len);\n\n    int* p = malloc(len * sizeof(int));        \n    int i = 0;\n    for (; i &lt; len; i++) {\n        p[i] = rand() % 100;\n        printf(&quot;%d,%#x\\n&quot;, p[i], &amp;p[i]);\n    }\n\n    if (p != NULL) {\n        free(p);\n        p = NULL;\n    }\n\n    getchar();\n}<\/code><\/pre>\n<pre><code class=\"language-c\">\/\/\u5185\u5b58\u6cc4\u9732\uff08p\u91cd\u65b0\u8d4b\u503c\u4e4b\u540e\uff0c\u518dfree\uff0c\u5e76\u6ca1\u6709\u771f\u6b63\u91ca\u653e\u5185\u5b58\uff09\nvoid main() {\n    int* p1 = malloc(1024 * 1024 * 10 * sizeof(int)); \/\/40M\n    \/\/free(p1);\n    \/\/p1 = NULL;\n    printf(&quot;%#x\\n&quot;, p1);\n\n    p1 = malloc(1024 * 1024 * 10 * sizeof(int) * 2); \/\/80M\n    printf(&quot;%#x\\n&quot;, p1);\n\n    free(p1);\n    p1 = NULL;\n\n    getchar();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u6001\u5206\u914d\u7c7b\u578b void main() { \/\/stack overflow\u9519\u8bef\uff0c\u6808\u6ea2\u51fa \/\/\u9759\u6001\u5185\u5b58\u5206\u914d in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-404","post","type-post","status-publish","format-standard","hentry","category-android-ndk"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=404"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/404\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}