C庫 - realloc() 函式



C 的stdlibrealloc() 函式用於重新分配動態分配記憶體的大小。它允許我們在釋放先前分配的記憶體後分配或預留一個新的記憶體塊。

realloc() 函式用於最佳化記憶體使用。如果我們發現分配的記憶體太大或太小,我們可以使用 realloc() 來調整分配記憶體的大小。

此函式僅在使用 realloc() 函式之前已動態分配記憶體時才有效。

語法

以下是 realloc() 函式的 C 庫語法:

void *realloc(void *ptr, size_t size)

引數

此函式接受以下引數:

  • ptr - 它表示要重新分配的記憶體塊的指標。該指標先前由 'malloc()'、'calloc()' 和 'realloc()' 分配。

  • size - 它表示記憶體塊的新大小(以位元組為單位)。如果大小為零,且指標非空,則會分配一個新的最小大小的物件,原始物件將被釋放。

返回值

此函式返回指向新分配記憶體的指標。如果函式未能分配請求的記憶體塊,則返回 'NULL'。

示例 1

在此示例中,我們在沒有動態分配記憶體的情況下使用 realloc() 函式。

#include <stdio.h>
#include <stdlib.h>
int main() 
{ 
   // initialize an array of size 10
   int arr[10];
   // store the array to pointer
   int *ptr = arr;
   // new pointer for reallocating 
   int *new_ptr; 
	
   arr[0] = 5; 
   arr[1] = 10;	 
	
   // use the new pointer  
   new_ptr = (int *)realloc(ptr, sizeof(int)*5); 
   *(new_ptr + 2) = 30; 
   
   int i;	
   for(i = 0; i < 5; i++){
      printf("%d ", *(new_ptr + i)); 	
   }
   
   // use getchar to get the charcter
   // if memory is allocated
   getchar(); 
   return 0; 
} 

輸出

以下是我們在沒有動態分配記憶體的情況下使用 realloc() 時的輸出。

warning: 'realloc' called on unallocated object 'arr' [-Wfree-nonheap-object]
new_ptr = (int *)realloc(ptr, sizeof(int)*5);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~
note: declared here
int arr[10];

Segmentation fault

示例 2

下面的示例使用 realloc() 來增加記憶體塊的大小。

#include <stdio.h>
#include <stdlib.h>

int main() {
   // Initially allocate memory for 2 integers
   int *ptr = malloc(2 * sizeof(int));
   if (ptr == NULL) {
      fprintf(stderr, "Memory allocation failed\n");
      return 1;
   }

   ptr[0] = 5;
   ptr[1] = 10;

   // increase the size of the integer
   int *ptr_new = realloc(ptr, 3 * sizeof(int));
   if (ptr_new == NULL) {
      fprintf(stderr, "Memory reallocation failed\n");
      free(ptr);
      return 1;
   }

   ptr_new[2] = 15;
   
   int i;
   // Display the array
   for (i = 0; i < 3; i++) {
      printf("%d ", ptr_new[i]);
   }

   free(ptr_new);
   return 0;
}

輸出

以下是輸出:

5 10 15

示例 3

讓我們建立另一個示例,我們先將陣列元素動態儲存到指標中,然後將陣列大小增加 7。

#include <stdio.h>
#include <stdlib.h>

int main() {
   int *ptr, i;
   
   printf("Initial size of the array is 4\n");
   
   // Allocate memory for 4 integers
   ptr = (int*)calloc(4, sizeof(int));
   
   if(ptr == NULL) {
      printf("Memory allocation failed");
      exit(1);
   }
   
   // initial array
   int initialArray[4] = {1, 2, 3, 4};
   
   // Copy array into ptr
   for(i = 0; i < 4; i++) {
      ptr[i] = initialArray[i];
   }
   
   printf("Increasing the size of the array by 3 elements ...\n");
   
   // Increase the size of the array to 7 elements
   // use realloc
   ptr = (int*)realloc(ptr, 7 * sizeof(int));
   
   if(ptr == NULL) {
      printf("Memory allocation failed");
      exit(1); 
   }
   
   // additional elements
   int additionalElements[3] = {5, 6, 7};
   
   // Copy additional elements into ptr
   for(i = 4; i < 7; i++) {
      ptr[i] = additionalElements[i - 4];
   }
   
   printf("Final array: \n");
   
   // display the final array
   for(i = 0; i < 7; i++) {
      printf("%d ", *(ptr+i));
   }
   free(ptr);
   return 0;
}

輸出

以下是輸出:

Initial size of the array is 4
Increasing the size of the array by 3 elements ...
Final array:
1 2 3 4 5 6 7
廣告