c的malloc那些事

c的malloc那些事

  • c的mallocfree
  • c++的newfree

c的mallocfree

先来聊一聊c /c++的内存分配

关于memory management, 在wikibooks 上的一段话我觉得很好,原文记下来:

The C programming language manages memory statically, automatically, or dynamically. Static-duration variables are allocated in main (fixed) memory and persist for the lifetime of the program; automatic-duration variables are allocated on the stack and come and go as functions are called and return. For static-duration and, before C99 (which allows variable-length automatic arrays[1]), automatic-duration variables, the size of the allocation is required to be compile-time constant. If the required size is not known until run-time (for example, if data of arbitrary size is being read from the user or from a disk file), then using fixed-size data objects is inadequate.
The lifetime of allocated memory is also a concern. Neither static- nor automatic-duration memory is adequate for all situations. Automatic-allocated data cannot persist across multiple function calls, while static data persists for the life of the program whether it is needed or not. In many situations the programmer requires greater flexibility in managing the lifetime of allocated memory.
These limitations are avoided by using dynamic memory allocation in which memory is more explicitly (but more flexibly) managed, typically, by allocating it from the heap, an area of memory structured for this purpose. In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.

大意就是在讲c语言的内存管理方式主要是三种,静态、自动的(局部变量的栈内存)、动态的。 静态内存
成员生命周期是程序的整个生命周期的。
自动的变量
放在栈内存上的,由函数调用和返回(释放)。分配的内存的大小要求是一个编译时确定的常量。
动态分配的变量
分配在堆上,为了在函数和函数互相间传递的方便而引出的一种变量,由malloc返回的指针来索引,使用完之后用free函数来释放指针指向的内存(但是该指针还是指向那个位置),块内存的大小是由运行时确定的。
推荐用法
#define MALLOC(p,s)   /*Here p & s are 2 integer pointer & variable respectively. */ \
if(!((p)=malloc(s))) \
{ \
fprintf(stderr,"Insufficient memory");   /* Prints the necessary error statement. */ \
exit(EXIT_FAILURE);   /* Exits <code>malloc</code> as there is insufficient memory. */ \
}

因此使用一个malloc的方法则是:

MALLOC(ptr,sizeof(int));

这里要强调的是记得使用sizeof这个函数,避免自己手动输入内存的大小,防止发生错误。

关于malloc的补充

在c中,malloc返回值是一个void *,要知道这才c++中是比较危险的(c++不支持指针的隐式转换)。而wikibooks上针对这个也有比较有趣的优点和缺点的讨论,有兴趣的可以google一下那方面的内容。

如果堆内存满了,则会返回空指针,至于NULL, 0, nullptr的区别可以看我的另外一篇博客。这还有另外一层意思,就是无论什么时候用关于malloc返回的指针前一定要判断指针的存在性:

void* ptr = malloc(size);
if(ptr)
{
//do sth

free(ptr);
}

c++的new 和 free

首先要强调的一点是,c中的malloc,free是函数,而c++中的new,delete是内置操作符,c++中鼓励用newdelete,因为他们可以对object进行初始化和析构,而mallocfree则只是分配给你那部分的内存空间而已。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值