关于malloc() and free()_Andy_Issta_新浪博客

Allocates memory blocks.

void *malloc( size_t size );The size_ttypedef is an unsigned int   size : to allocate..  Return Value:

malloc returns a void pointerto the allocated space or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return frommalloc, even if the amount of memory requested is small.

Deallocates or frees a memory block.

void free( void *memblock );

memblock:Previously allocated memory block to be freed.

Thefreefunction deallocates a memory block (memblock) that was previously allocated by a call tocalloc,malloc, orrealloc. The number of freed bytes is equivalent to the number of bytes requested when the block was allocated (or reallocated, in the case ofrealloc). IfmemblockisNULL, the pointer is ignored andfreeimmediately returns. Attempting to free an invalid pointer (a pointer to a memory block that was not allocated bycalloc,malloc, orrealloc) may affect subsequent allocation requests and cause errors.

-----------------------------------------------------------------------------------------------------------------

void *malloc(long NumBytes):该函数分配了NumBytes个,并返回了指向这块内存的指针。如果
分配失败,则返回一个空指针(NULL)。
关于分配失败的原因,应该有多种,比如说空间不足就是一种。
void free(void *FirstByte): 该函数是将之前用malloc分配的空间还给程序或者是操作系统,也就
是释放了这块内存,让它重新得到自由。
-------------------------------------------------------------------------------------------------------------------

原型:extern void *malloc(unsigned int num_bytes);
用法:#include <malloc.h>
或#include<stdlib.h>
功能:分配长度为num_bytes字节的内存块
说明:如果分配成功则返回指向被分配内存的指针,否则返回空指针NULL。
当内存不再使用时,应使用free()函数将内存块释放。
malloc的语法是:指针名=(数据类型*)malloc(长度),(数据类型*)表示指针.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

issta

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值