C 语言动态内存分配

众所周知,数组是固定数量值的集合。一旦声明了数组的大小,就无法更改。

有时声明的数组大小可能不够。为了解决这个问题,您可以在运行时手动分配内存。这被称为 C 编程中的动态内存分配。

为了动态分配内存,可以使用库函数 malloc()calloc()realloc()free()。这些函数定义在 <stdlib.h> 头文件中。


C malloc()

malloc 的名字代表内存分配 (memory allocation)。

malloc() 函数会预留指定字节数的内存块。它会返回一个 void 指针,可以被强制转换为任何形式的指针。


malloc() 的语法

ptr = (castType*) malloc(size);

示例

ptr = (float*) malloc(100 * sizeof(float));

上面的语句分配了 400 字节的内存。这是因为 float 的大小是 4 字节。指针 ptr 保存着已分配内存中第一个字节的地址。

如果内存无法分配,该表达式将返回一个 NULL 指针。


C calloc()

calloc 的名字代表连续分配 (contiguous allocation)。

malloc() 函数分配内存,但不初始化内存,而 calloc() 函数分配内存并用零初始化所有位。


calloc() 的语法

ptr = (castType*)calloc(n, size);

示例

ptr = (float*) calloc(25, sizeof(float));

上面的语句为 25 个 float 类型元素分配了连续的内存空间。


C free()

使用 calloc()malloc() 动态分配的内存不会自行释放。您必须显式使用 free() 来释放空间。


free() 的语法

free(ptr);

此语句释放由 ptr 指向的内存中已分配的空间。


示例 1:malloc() 和 free()

// 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;
}

输出

Enter number of elements: 3
Enter elements: 100
20
36
Sum = 156

在这里,我们为 nint 动态分配了内存。


示例 2:calloc() 和 free()

// 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;
}

输出

Enter number of elements: 3
Enter elements: 100
20
36
Sum = 156

C realloc()

如果动态分配的内存不足或超过所需,您可以使用 realloc() 函数更改先前分配的内存的大小。


realloc() 的语法

ptr = realloc(ptr, x);

这里,ptr 被重新分配为新的大小 x


示例 3:realloc()

#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:\n");
  for(i = 0; i < n1; ++i)
    printf("%pc\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:\n");
  for(i = 0; i < n2; ++i)
    printf("%pc\n", ptr + i);
  
  free(ptr);

  return 0;
}

输出

Enter size: 2
Addresses of previously allocated memory:
26855472
26855476

Enter the new size: 4
Addresses of newly allocated memory:
26855472
26855476
26855480
26855484
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战