(转) Dynamic memory
2014-02-05 17:39
363 查看
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
newand
delete.
Operators new and new[]
Dynamic memory is allocated using operatornew.
newis 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 //allocate memory to contain one single element of type [code]typepointer = new type [number_of_elements] //allocate a block (an array) of elements of type
type
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_elementsis an integer value representing the amount of these. For example:
int * foo; foo = new int [5];
In this case, the system dynamically allocates space for five elements of type
intand returns a pointer to the first element of the sequence, which is assigned to
foo(a pointer). Therefore,
foonow points to a valid block of memory with space for five elements of type
int.
![](http://www.cplusplus.com/doc/tutorial/dynamic/dynamic.png)
first element | second element |
foo[0] 或者 *foo | foo[1] 或者 *(foo+1) |
foo
Here,
foois a pointer, and thus, the first element pointed to by
foocan 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...
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
newallows 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
neware 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_allocis 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:
foo = new int [5]; // if allocation fails, an exception is thrown
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_allocexception or terminating the program, the pointer returned by
newis 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:
foo = new (nothrow) int [5]; |
foois a null pointer:
1 2 3 4 5 | int * foo; foo = new (nothrow) int [5];if (foo == nullptr) { // error assigning memory. Take measures. } |
nothrowmethod 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
nothrowmechanism 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 operatordelete, whose syntax is:
delete pointer; delete[] pointer;
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
deleteshall be either a pointer to a memory block previously allocated with
new, or a null pointer (in the case of a null pointer,
deleteproduces no effect).
// rememb-o-matic #include <iostream> #include <new> using namespace std; int main () { int i,n; int * p; cout << "How many numbers would you like to type? "; cin >> i; p= new (nothrow) int[i]; if (p == nullptr) cout << "Error: memory could not be allocated"; else { for (n=0; n<i; n++) { cout << "Enter number: "; cin >> p ; } cout << "You have entered: "; for (n=0; n<i; n++) cout << p << ", "; delete[] p; } return 0; } | How many numbers would you like to type? 5 Enter number : 75 Enter number : 436 Enter number : 1067 Enter number : 8 Enter number : 32 You have entered: 75, 436, 1067, 8, 32, |
i), not a constant expression:
p= new (nothrow) int[i]; |
iso 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 operatorsnewand
deletefor allocating dynamic memory. But these were not available in the C language; instead, it used a library solution, with the functions
malloc,
calloc,
reallocand
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.
相关文章推荐
- uva 544(kruskal 变形)
- 浅谈设计模式--建造器模式(Builder Pattern)
- Mac OS X 10.9 文件(资源)选取窗长时间加载bug的修复
- 关于内存对界
- Java面向对象编程 (Java 类的基本构成)
- 线程同步1——synchronized
- vim 安装 Jedi-vim 报错(E121: Undefined variable: g:jedi#call_signature_escape)
- C 中标准库函数 qsort 的用法
- SOS
- DIY远程控制开关(tiny6410+LED+yeelink+curl)
- 关于编码的一切
- 1061成绩排序(c中qsort,strcmp函数的使用)
- HDU 2138 How many prime numbers
- 处理散列冲突的方法 - 数据结构和算法85
- 紫外线消毒器水处理杀菌的四大优势分析
- JQuery Introduction
- shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such fi
- C#.NET 大型通用信息化系统集成快速开发平台 4.1 版本 - 对外不要提供Delete方法加强软件的安全性
- [floyd]HDU 1181变形课
- C++回顾之引用