Memory Management in C++

Memory Management

Use the same form in corresponding uses of new and delete

eryar@163.com

对应的newdelete要采用相同的形式

new operator时会发生两件事:首先,内存通过operator new被分配;然后,为被分配的内存调用一个或多个构造函数。

delete operator时也会发生两件事:首先,为将释放的内存调用一个或多个析构函数;然后,通过operator delete释放内存。

对于delete operator来说会有这样一个重要的问题:内存中有多少个对象需要被删除呢?删除多了,程序也就崩溃了;删除少了,就有内存未被正确释放。这个问题简单来说就是:要被删除的指针指向是单个对象,还是对象数组?这只有你来告诉delete operator。如果你在用delete operator时没有括号,delete就会认为指向的是单个对象;否则它就会认为指向的是一个数组。

Prefer new and delete to malloc and free

mallocfree(及其变体)会产生问题的原因在于它们太简单:他们不知道构造函数和析构函数。(有时越简单越好。)

假设用两种方法给一个包含10string对象的数组分配空间,一个用malloc,另一个用new

其结果是,stringarray1确实指向的是可以容纳10string对象的足够空间,但内存里并没有创建这些对象。当释放这些内存时,你一定会这么做:

调用free将会释放stringarray1指向的内存,但内存里的string对象不会调用析构函数。如果string对象象一般情况那样,自己已经分配了内存,那这些内存将会全部丢失。相反,当对stringarray2使用delete时,数组里的每个string对象都会在内存释放前调用析构函数。即然newdelete可以这么有效地与构造函数和析构函数交互,选用它们是显然的。

new/deletemalloc/free混用也是个坏想法。对一个用new获取来的指针调用free,或者对一个用malloc获取来的指针调用delete,其后果是不可预测的。

示例程序:


  1:
  2: #include <iostream>
  3: using namespace std;
  4:
  5: class CTest
  6: {
  7: public:
  8:     CTest() { cout<<"Default constructor."<<endl; }
  9:     ~CTest() { cout<<"Default destrcutor."<<endl; }
 10:
 11: };
 12:
 13: int main(int argc, char *argv[])
 14: {
 15:     cout<<"=== Test new/delete begin ==="<<endl;
 16:     CTest* pTestNewDelete = new CTest;
 17:     delete pTestNewDelete;
 18:     cout<<"=== Test new/delete end   ==="<<endl;
 19:
 20:     cout<<"~~~ Test malloc/free begin ~~~"<<endl;
 21:     CTest* pTestMallocFree = static_cast<CTest*> (malloc(sizeof(CTest)));
 22:     free(pTestMallocFree);
 23:     cout<<"~~~ Test malloc/free end   ~~~"<<endl;
 24:
 25:     return 0;
 26: }
 27: 

输出:

1 === Test new/delete begin ===
2 Default constructor.
3 Default destrcutor.
4 === Test new/delete end   ===
5 ~~~ Test malloc/free begin ~~~
6 ~~~ Test malloc/free end   ~~~

来源:
1. Scott Meyers Effective C++

 

PDF Version Memory Management in C++

时间: 2024-12-15 05:08:48

Memory Management in C++的相关文章

Operating Principle and Implementation of Flink: Memory Management

Nowadays, open-source big data frameworks (such as Hadoop, Spark and Storm) all employ JVM, and Flink is one of them. JVM-based data analysis engines all need to store a large amount of data in the memory, so they have to address the following JVM is

memory management unit (MMU)

A memory management unit (MMU) is a computer hardware component that handles all memory and caching operations associated with the processor. In other words, the MMU is responsible for all aspects of memory management. It is usually integrated into t

Oracle Memory Management and HugePage

在这篇文章中,我给大家介绍一些Oracle内存管理和大页的知识.Oracle发展这么多年,提供了多种的内存管理方式,从最早SGA.PGA手工管理,到9I版本出现的PGA的自动管理,到10G版本出现的SGA自动管理(ASMM),再到11G版本出现的memory自动管理(AMM),Oracle基本是在朝着智能化.傻瓜化.自动化的方向稳步前进着,对于初学Oracle的DBA来说,看到这些不同的内存管理方式一定心里有着不同程度的疑惑,例如: ·          Oracle有这么多内存分配的管理方式,

Win8.1蓝屏提示错误Memory Management并重启怎么办

  解决方法 1. 退出所有基于 Windows 的程序. 2. 单击开始或者用win+R组合键,在开始搜索框中,键入regedit ,然后按 ENTER 键或者确定.(如果您收到用户帐户控制对话框,请单击继续.) 3. 找到并单击以下注册表子项之一: HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/Msahci HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/IastorV 4.

On Memory Leaks in Java and in Android.

from:http://chaosinmotion.com/blog/?p=696 Just because it's a garbage collected language doesn't mean you can't leak memory or run out of it. Especially on Android where you get so little to begin with. Now of course sometimes the answer is that you

oracle数据库提示ORA-27102: out of memory问题处理

公司的一台oracle数据库在启动时候报错ORA-27102: out of memory ,具体如下: 现象: alter system set sga_max_size=12884901888 scope=spfile      ---(12G) 出现:  SQL >startup force ORA-27102: out of memory Linux-x86_64 Error: 28: No space left on device 网上查询ORA-27102报错代码的解决方法,发现为s

Off-heap Memory in Apache Flink and the curious JIT compiler

Running data-intensive code in the JVM and making it well-behaved is tricky. Systems that put billions of data objects naively onto the JVM heap face unpredictable OutOfMemoryErrors and Garbage Collection stalls. Of course, you still want to to keep

How To Debug Memory Leaks with XCode and Instruments Tutorial【转】

This is the second article in a three-part series on working with memory in Objective-C on the iPhone. In the first part of the series, we covered how to manage memory in Objective-C by using instance variables and reference counting. No matter how w

Instruments Tutorial for iOS: How To Debug Memory Leaks【转】

If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter. Thanks for visiting! Call the plumber, it's-a-leaking! Update 4/12/13: These days, you should probably be using Apple's new Automatic Reference Counting (ARC) techn