<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # C 動態內存分配 > 原文: [https://www.programiz.com/c-programming/c-dynamic-memory-allocation](https://www.programiz.com/c-programming/c-dynamic-memory-allocation) #### 在本教程中,您將學習使用標準庫函數:`malloc()`,`calloc()`,`free()`和`realloc()`在 C 程序中動態分配內存。 如您所知,數組是固定數量的值的集合。 聲明數組的大小后,您將無法更改它。 有時,您聲明的數組的大小可能不足。 要解決此問題,可以在運行時手動分配內存。 這在 C 編程中稱為動態內存分配。 為了動態分配內存,使用了`malloc()`,`calloc()`,`realloc()`和`free()`這些庫函數。 這些函數在`<stdlib.h>`頭文件中定義。 * * * ## C `malloc()` 名稱`malloc`代表內存分配。 `malloc()`函數保留指定字節數的內存塊。 并且,它返回`void`的[指針](/c-programming/c-pointers "C Pointers"),可以將其轉換為任何形式的指針。 * * * ### `malloc()`的語法 ```c ptr = (castType*) malloc(size); ``` **示例** ```c ptr = (float*) malloc(100 * sizeof(float)); ``` 上面的語句分配了 400 個字節的內存。 這是因為`float`的大小為 4 個字節。 并且,指針`ptr`保存分配的內存中第一個字節的地址。 如果無法分配內存,則表達式將產生`NULL`指針。 * * * ## C `calloc()` 名稱`calloc`代表連續分配。 `malloc()`函數分配內存,并保留未初始化的內存。 而`calloc()`函數分配內存并將所有位初始化為零。 * * * ### `calloc()`的語法 ```c ptr = (castType*)calloc(n, size); ``` **示例**: ```c ptr = (float*) calloc(25, sizeof(float)); ``` 上面的語句在內存中為`float`類型的 25 個元素分配了連續的空間。 * * * ## C `free()` 使用`calloc()`或`malloc()`創建的動態分配內存不會自行釋放。 您必須顯式使用`free()`釋放空間。 * * * ### `free()`的語法 ```c free(ptr); ``` 該語句釋放`ptr`指向的內存中分配的空間。 * * * ### 示例 1:`malloc()`和`free()` ```c // Program to calculate the sum of n numbers entered by the user #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) malloc(n * sizeof(int)); // if memory cannot be allocated if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); // deallocating the memory free(ptr); return 0; } ``` 在這里,我們為`n`個`int`的數量動態分配了內存。 * * * ### 示例 2:`calloc()`和`free()` ```c // Program to calculate the sum of n numbers entered by the user #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) calloc(n, sizeof(int)); if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); free(ptr); return 0; } ``` * * * ## C `realloc()` 如果動態分配的內存不足或超出要求,則可以使用`realloc()`函數更改先前分配的內存大小。 * * * ### `realloc()`的語法 ```c ptr = realloc(ptr, x); ``` 在此,以新的大小`x`重新分配`ptr`。 * * * ### 示例 3:`realloc()` ```c #include <stdio.h> #include <stdlib.h> int main() { int *ptr, i , n1, n2; printf("Enter size: "); scanf("%d", &n1); ptr = (int*) malloc(n1 * sizeof(int)); printf("Addresses of previously allocated memory: "); for(i = 0; i < n1; ++i) printf("%u\n",ptr + i); printf("\nEnter the new size: "); scanf("%d", &n2); // rellocating the memory ptr = realloc(ptr, n2 * sizeof(int)); printf("Addresses of newly allocated memory: "); for(i = 0; i < n2; ++i) printf("%u\n", ptr + i); free(ptr); return 0; } ``` 運行該程序時,輸出為: ```c Enter size: 2 Addresses of previously allocated memory:26855472 26855476 Enter the new size: 4 Addresses of newly allocated memory:26855472 26855476 26855480 26855484 ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看