堆内存管理

堆内存管理

C标准库中提供一套管理堆内存的函数,这些函数底层封装了各操作系统的堆内存管理接口,所以可以跨平台使用,这些函数声明在 stdlib.h 头文件中。

1.malloc

先看看便准库的描述:

The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can laterbe successfully passed to free().

void *malloc(size_t size);
功能:向malloc申请size字节的堆内存块
size:
    要申请的内存块字节数
    如果申请数组形式的内存块,size=sizeof(数组元素类型)*数组长度
返回值:
	如果申请成功,则返回内存块首地址
    失败申请失败,则返回NULL,现在有堆内存无法满足size个字节的需求

注意:

​ 1、使用malloc申请到的内存块,里面的内容是不确定的,malloc不会帮我们初始化,可以使用bzero,memset函数进行初始化。

​ 2、如果size等于0,返回NULL或唯一的一个地址,并且该地址可以通过free释放而不出错,但不能使用它指向的内存。

2.calloc

The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

void *calloc(size_t nmemb, size_t size);
功能:申请nmemb个size个字节的内存块,专门用于申请数组型的内存块。
nmemb:数组的长度
size:数组元素的字节数
返回值:与malloc相同
注意:使用calloc申请的内存块,所有字节会被初始化0
3.realloc

The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.

void *realloc(void *ptr, size_t size);
功能1:把已有的堆内存块调小
    ptr是malloc、calloc、realloc的返回值,也就已有堆内存块首地址
    size < oldsize 
    此时不需要关心realloc的返回值
    
功能2:把已有的堆内存块调大
    情况1:如果ptr后续的内存没有被占用,realloc会在ptr的基础上进行扩大
    情况2:如果ptr后续的内存已经被占用,realloc会重新分配一块符合要求的内存块,并把ptr上的内容拷贝到新的内存块,然后释放ptr,再返回新内存块的首地址
   	使用此功能时,我们必须重新接收realloc函数的返回值,我们无法预料realloc执行的是情况1还是情况2。
    
功能3:释放内存
	0==size,此时realloc的功能就相当于free
    
功能4:申请内存
    NULL==ptr,0<size
    此时的功能就相当于malloc
4.free

The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc(), or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is NULL, no operation is performed.

void free(void *ptr);
功能:释放堆内存 
ptr:要释放的内存块的首地址,它必须是malloc、calloc函数的返回值

注意:

1、不要重复释放 否则会出现
在这里插入图片描述

所以,第一次释放内存后,要把指针及时的赋值为空,防止重复释放产生的错误。

2、free的参数可以是空指针,不会出现错误,也不会执行任何操作,也能看出空指针比野指针安全。

内存后,要把指针及时的赋值为空,防止重复释放产生的错误。

2、free的参数可以是空指针,不会出现错误,也不会执行任何操作,也能看出空指针比野指针安全。

3、free释放的是使用权,也就是说不会改变指针的值。只是将之前分配给自己的内存还给操作系统,操作系统可以对这块内存再次分配了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值