Skip to content

动态内存分配

在使用数组的时候我们有时候不知道需要多大的数组,于是提前申请一个大的数组用来存储数据,但是这回使得大块的内存被浪费

malloc和free

C
void *malloc(size_t size);
void free(void *point);

malloc会申请一块内存,实际上有可能比你申请的大一点,如果申请失败。返回NULL,free的参数是NULL或者malloc的返回值

在对边界要求严格的机器上,返回的地址会满足要求最严格的数据的地址

calloc和realloc

C
void *calloc(size_t num_elements, size_t element_size);
void *realloc(void *ptr, size_t new_size);

calloc:也是用来申请一段内存的,和malloc的区别是他会把申请到的内存初始化为0,但是如果你是想把数据存储到一块内存之中,这个行为就是浪费时间,请求的方式也不同,他的参数元素的数量以及元素的大小

**realloc:**修改已经有的一块内存的大小,可以把一块扩大或者缩小,在扩展时候如果需要更换内存,会把原来的数据复制

到新的内存,返回新的地址

使用分配到的内存

先判断返回的是不是NULL

使用一个有类型的指针去接受返回的指针 ===> 得到一块数组

常见的错误

  • 对NULL进行解读
  • 操作时候越界
  • 释放不是分配的内存
  • 释放一部分
  • 释放之后继续使用
C
  1 #include <stdlib.h>                                                                   
  2 
  3 #define malloc//禁止使用malloc
  4 #define MALLOC(num,type) (type *)alloc((num) * sizeof(type))//对函数进行重定义
  5 extern void *alloc(size_t size);




  1 #include <stdio.h>                                                                    
  2 #include "malloc.h"
  3 #undef malloc
  4 
  5 void *alloc(size_t size)
  6 {
  7     void *new_mem;
  8     new_mem = malloc(size);
  9     if(new_mem == NULL)//进行检查
 10     {
 11         printf("Out of memory");
 12         exit(1);
 13     }
 14     return new_mem;
 15 }


  1 #include "malloc.h"                                                                   
  2 
  3 int main(void)
  4 {
  5     int *new_memory;
  6     new_memory  = MALLOC(25, int);
  7     printf("%p\n", new_memory);
  8     free(new_memory);
  9     return 0;
 10 }

free在释放内存的时候必须全部释放,但是可以用realloc可以调整

内存泄漏

申请的内存没有及时的释放

C
  1 #include <stdio.h>                                                                    
  2 #include <stdlib.h>
  3 
  4 int compare_integers(void const *a, void const *b)
  5 {
  6     register int const *pa = a;
  7     register int const *pb = b;
  8     return *pa > *pb ? 1: *pa < *pb ?-1 : 0;
  9 }
 10 
 11 int main(void)
 12 {
 13     int *array;
 14     int n_value;
 15     int i;
 16 
 17     printf("How many values are there?");
 18     if(scanf("%d", &n_value) != 1|| n_value <=0 )
 19     {
 20         printf("Illeage number of value \n");
 21         exit(EXIT_FAILURE);
 22     }
 23 
 24     array = malloc(n_value * sizeof(int));
 25     if(array == NULL){
 26         printf("Can't get memory for that many value.\n");
 27         exit(EXIT_FAILURE);
 28     }
 29 
 30     for(i = 0;i < n_value; i++)
 31     {
 32         printf("?  ");
 33         if(scanf("%d", array + i) != 1)//输入要保存的数据
 34         {
 35             printf("Error reading vlue #%d\n", i);
 36             free(array);
 37             exit(EXIT_FAILURE);
 38         }
 39     }
 40     
 41     qsort(array, n_value, sizeof(int), compare_integers); //按照规定的顺序对数组进行排列
 42 
 43     for(i=0;i<n_value; i++)
 44     {
 45         printf("%d\n", array[i]);
 46     }
 47     free(array);
 48     return EXIT_SUCCESS;
 49 
 50 }

复制字符串,用于读取大文件的信息保存

C
  1 #include <stuio.h>
  2 #include <string.h>
  3 
  4 char *strdup(char const *string)
  5 {
  6     char *new_string;
  7     new_string = malloc(strlen(string) + 1);
  8     if(new_string != NULL)
  9         strcpy(new_string , string);
 10         return new_string;                                                            
 11 }