malloc函数 链表 运行时才知道内存 动态内存
https://baike.baidu.com/item/malloc函数
中文名
动态内存分配
外文名
memory allocation
简 称
malloc
原 型
extern void *malloc
头文件
stdlib.h
工作机制
malloc函数的实质体现在,它有一个将可用的内存块连接为一个长长的列表的所谓空闲链表。调用malloc函数时,它沿连接表寻找一个大到足以满足用户请求所需要的内存块。然后,将该内存块一分为二(一块的大小与用户请求的大小相等,另一块的大小就是剩下的字节)。接下来,将分配给用户的那块内存传给用户,并将剩下的那块(如果有的话)返回到连接表上。调用free函数时,它将用户释放的内存块连接到空闲链上。到最后,空闲链会被切成很多的小内存片段,如果这时用户申请一个大的内存片段,那么空闲链上可能没有可以满足用户要求的片段了。于是,malloc函数请求延时,并开始在空闲链上翻箱倒柜地检查各内存片段,对它们进行整理,将相邻的小空闲块合并成较大的内存块。如果无法获得符合要求的内存块,malloc函数会返回NULL指针,因此在调用malloc动态申请内存块时,一定要进行返回值的判断。
Linux Libc6采用的机制是在free的时候试图整合相邻的碎片,使其合并成为一个较大的free空间。
https://baike.baidu.com/item/链表
Dynamic memory - C++ Tutorials http://www.cplusplus.com/doc/tutorial/dynamic/
Dynamic memory
In the programs seen in previous chapters, all memory needs were determined before program execution by defining the variables needed. But there may be cases where the memory needs of a program can only be determined during runtime. For example, when the memory needed depends on user input. On these cases, programs need to dynamically allocate memory, for which the C++ language integrates the operators new
and delete
.
Operators new and new[]
Dynamic memory is allocated using operator new
. new
is followed by a data type specifier and, if a sequence of more than one element is required, the number of these within brackets []
. It returns a pointer to the beginning of the new block of memory allocated. Its syntax is: pointer = new type
pointer = new type [number_of_elements]
The first expression is used to allocate memory to contain one single element of type type
. The second one is used to allocate a block (an array) of elements of type type
, where number_of_elements
is an integer value representing the amount of these. For example:
|
|
In this case, the system dynamically allocates space for five elements of type int
and returns a pointer to the first element of the sequence, which is assigned to foo
(a pointer). Therefore, foo
now points to a valid block of memory with space for five elements of type int
.
Here, foo
is a pointer, and thus, the first element pointed to by foo
can be accessed either with the expression foo[0]
or the expression *foo
(both are equivalent). The second element can be accessed either with foo[1]
or *(foo+1)
, and so on...
There is a substantial difference between declaring a normal array and allocating dynamic memory for a block of memory using new
. The most important difference is that the size of a regular array needs to be a constant expression, and thus its size has to be determined at the moment of designing the program, before it is run, whereas the dynamic memory allocation performed by new
allows to assign memory during runtime using any variable value as size.
The dynamic memory requested by our program is allocated by the system from the memory heap. However, computer memory is a limited resource, and it can be exhausted. Therefore, there are no guarantees that all requests to allocate memory using operator new
are going to be granted by the system.
C++ provides two standard mechanisms to check if the allocation was successful:
One is by handling exceptions. Using this method, an exception of type bad_alloc
is thrown when the allocation fails. Exceptions are a powerful C++ feature explained later in these tutorials. But for now, you should know that if this exception is thrown and it is not handled by a specific handler, the program execution is terminated.
This exception method is the method used by default by new
, and is the one used in a declaration like:
|
|
The other method is known as nothrow
, and what happens when it is used is that when a memory allocation fails, instead of throwing a bad_alloc
exception or terminating the program, the pointer returned by new
is a null pointer, and the program continues its execution normally.
This method can be specified by using a special object called nothrow
, declared in header <new>
, as argument for new
:
|
|
In this case, if the allocation of this block of memory fails, the failure can be detected by checking if foo
is a null pointer:
|
|
This nothrow
method is likely to produce less efficient code than exceptions, since it implies explicitly checking the pointer value returned after each and every allocation. Therefore, the exception mechanism is generally preferred, at least for critical allocations. Still, most of the coming examples will use the nothrow
mechanism due to its simplicity.
Operators delete and delete[]
In most cases, memory allocated dynamically is only needed during specific periods of time within a program; once it is no longer needed, it can be freed so that the memory becomes available again for other requests of dynamic memory. This is the purpose of operator delete
, whose syntax is:
|
|
The first statement releases the memory of a single element allocated using new
, and the second one releases the memory allocated for arrays of elements using new and a size in brackets ([]
).
The value passed as argument to delete
shall be either a pointer to a memory block previously allocated with new
, or a null pointer (in the case of a null pointer, delete
produces no effect).
|
|
How many numbers would you like to type? 5 |
Notice how the value within brackets in the new statement is a variable value entered by the user (i
), not a constant expression:
|
|
There always exists the possibility that the user introduces a value for i
so big that the system cannot allocate enough memory for it. For example, when I tried to give a value of 1 billion to the "How many numbers" question, my system could not allocate that much memory for the program, and I got the text message we prepared for this case (Error: memory could not be allocated
).
It is considered good practice for programs to always be able to handle failures to allocate memory, either by checking the pointer value (if nothrow
) or by catching the proper exception.
Dynamic memory in C
C++ integrates the operators new
and delete
for allocating dynamic memory. But these were not available in the C language; instead, it used a library solution, with the functions malloc
, calloc
, realloc
and free
, defined in the header <cstdlib>
(known as <stdlib.h>
in C). The functions are also available in C++ and can also be used to allocate and deallocate dynamic memory.
Note, though, that the memory blocks allocated by these functions are not necessarily compatible with those returned by new
, so they should not be mixed; each one should be handled with its own set of functions or operators.
malloc函数 链表 运行时才知道内存 动态内存的更多相关文章
- C++ //多态 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 //动态多态:派生类和虚函数实现运行时多态
1 //多态 2 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 3 //动态多态:派生类和虚函数实现运行时多态 4 5 //静态多态和动态多态的区别 6 //静态多态的函数地址早 ...
- malloc函数 链表
https://baike.baidu.com/item/malloc函数 malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void ...
- Java内存区域(运行时数据区域)和内存模型(JMM)
Java 内存区域和内存模型是不一样的东西,内存区域是指 Jvm 运行时将数据分区域存储,强调对内存空间的划分. 而内存模型(Java Memory Model,简称 JMM )是定义了线程和主内存之 ...
- C++中的动态类型与动态绑定、虚函数、运行时多态的实现
动态类型与静态类型 静态类型 是指不需要考虑表达式的执行期语义,仅分析程序文本而决定的表达式类型.静态类型仅依赖于包含表达式的程序文本的形式,而在程序运行时不会改变.通俗的讲,就是上下文无关,在编译时 ...
- Java泛型函数的运行时类型检查的问题
在一个数据持久化处理中定义了数据保存和读取的 泛型函数的,但是在运行时出现类型转换错误,类型不匹配,出错的位置不是load方法,而是在调用load方法之后,得到了列表数据,对列表数据进行使用时出现的. ...
- 郑州尚学堂:链表的C语言如何实现动态内存分配
一.为什么用动态内存分配 但我们未学习链表的时候,如果要存储数量比较多的同类型或同结构的数据的时候,总是使用一个数组.比如说我们要存储一个班级学生的某科分数,总是定义一个float型(存在0.5分)数 ...
- MFC中关于运行时类信息及动态创建对象的两个宏的意义(转)
http://blog.csdn.net/ligand/article/details/49839507 MFC运行时类信息 用途: 程序在运行时,获取对象类的信息及类的继承关系 实现: 1.定义的类 ...
- CH12 动态内存
动态分配的对象的生命期与它们在哪里创建的五官,只有显示地释放时,这些对象才被销毁 静态内存用来保存局部static对象.类static数据成员以及定义在任何函数之外的变量,栈内存用来保存定义在函数内的 ...
- FreeRTOS 动态内存管理
以下转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 动态内存管理,动态内存管理是 FreeRTOS 非常重要的一项功能,前面 ...
随机推荐
- 理解Tomcat工作原理
WEB服务器 只要Web上的Server都叫Web Server,但是大家分工不同,解决的问题也不同,所以根据Web Server提供的功能,每个Web Server的名字也会不一样. 按功能分类,W ...
- udp聊天室--简易
package 聊天; /*一切随便消逝吧*/ import java.net.DatagramSocket; import java.net.SocketException; public clas ...
- Atcoder abc187 F Close Group(动态规划)
Atcoder abc187 F Close Group 题目 给出一张n个点,m条边的无向图,问删除任意数量的边后,留下来的最少数量的团的个数(\(n \le 18\) ) 题解 核心:枚举状态+动 ...
- 权限管理&用户组管理
权限管理&用户组管理 Linux用户介绍: 1.什么是用户? 用户对硬件资源的操作都需要通过操作系统,比如用户要读取硬盘中的一份关键数据 出于安全考虑,操作系统的开发者们都专门开发了安全机制, ...
- Head First 设计模式 —— 00. 引子
Head First 学习原则 P xxx 可视化:图片使得学习效率更高,更易懂 交谈式:第一人称交谈方式讲述学习内容更易引起注意 多思考:自主思考练习题和拓展知识的问题 保持注意力集中:将知识融合进 ...
- 借助Docker搭建JMeter+Grafana+Influxdb监控平台
我们都知道Jmeter提供了原生的结果查看,既然有原生的查看结果,为什么还要多此一举使用其他工具进行查看呢,除了查看内容丰富外还有最主要的原因:Jmeter提供的查看结果插件本身是比较消耗性能的,所以 ...
- 【C++】《Effective C++》第四章
第四章 设计与声明 条款18:让接口容易被正确使用,不易被误用 请记住 好的接口很容易被正确使用,不容易被误用.你应该在你的所有接口中努力达到这些性质. "促进正确使用"的办法包括 ...
- 【C++】《C++ Primer 》第三章
第三章 字符串.向量和数组 一.命名空间的using声明 使用某个命名空间:例如 using std::cin表示使用命名空间std中的名字cin. 头文件的代码一般不应该使用using声明,这是因为 ...
- 有关Servlet的面试题
CGI(Common Gateway Interface),通用网管接口 通用网管接口,简称CGI,是一种根据请求信息动态产生回应内容的技术.通过CGI,web服务器可以根据请求的不同,启动不同的外部 ...
- 【Linux】history用法
通过history命令可以查看我们在系统中输入过的命令 history命令的一些常用参数 -c 清空内存中命令历史 -d # 删除指定的历史命令,比如 history -d 100 ,就是删除第1 ...