the leak of the memory in c++ 03

The Leak of the Memory in C++

In this chaper I will introduce a new smart pointer which is scoped_ptr;

It likes auto_ptr but better. When peopel use auto_ptr, sometimes they forget

that auto_ptr alread is empty, like this;

 auto_ptr<Person> p1(new Person);
    auto_ptr<Person> p2 = p1;
    cout << p1->name() << endl;

In above situation, p1 is empty pointer after it gives pointee to p2. It

will produce some error.

But scoped_ptr can‘t give pointee to any pointer, so it will be better and

safer in some cases.

Now I‘ll introduce some special pattern to you, I say special, because in

normal cases, it just used in C++; In articles which I wrote before, I avoided

to use operator of delete, but I still use new operator, in this article I‘ll

introduce method which can help us to avoid use new operator.

Now let us welcome our pattern: PIMPL, it means Pointer to Implement. It

looks like below.

PersonImpl.h

class PersonImpl
{
public:
    PersonImpl()
    {
        cout << "I'm constructed" << endl;
    }
    ~PersonImpl()
    {
        cout << "I'm destructed!" << endl;
    }

private:
    int age_;
    string name_;
public:
    const string& getName()const
    {
        return name_;
    }
    void setName(const string& name)
    {
        name_ = name;
    }

    int getAge()
    {
        return age_;
    }

    void setAge(int age)
    {
        age_ = age;
    }
};

Person.h

using namespace std;
using namespace boost;

class Person
{

public:
    Person() :
        personPtr_(new PersonImpl)
    {}

    ~Person()
    {
    };
private:
    scoped_ptr<PersonImpl> personPtr_;

public:
    int getAge()
    {
        return personPtr_->getAge();
    }

    void setAge(int age)
    {
        personPtr_->setAge(age);
    }

    const string& getName()const
    {
        return personPtr_->getName();
    }

    void setName(const string& name)
    {
        personPtr_->setName(name);
    }
};

main.cpp

using namespace std;
using namespace boost;

int main(int,char**)
{
    Person person;
    return 0;
}

In above code, I did not implement Person directly, instead of a smart

pointer which point a class which implement Person. So we can use class Person

just like I use it in main function without new neither delete operators.

the leak of the memory in c++ 03

时间: 2024-12-09 03:09:49

the leak of the memory in c++ 03的相关文章

The Leak of The Memory in C++ 1.2

伸展树模版真的好长好长... cut a b c:把第a-1个数伸展到根节点,把第b+1个数伸展到a的右子树,然后把ch[ch[root][1][0]]拿掉,放在剩下的树的第c个节点下. flip a b:把第a-1个数伸展到根节点,把第b+1个数伸展到a的右子树,然后翻转ch[ch[root][1][0]]: 由于会出现操作两边的情况,所以加了两个-1节点. 注意: 1,输出的时候要注意空格和换行. 2,在拿掉子树的时候要注意push_up(); #include<stdio.h> #inc

Linux C/C++ Memory Leak Detection Tool

目录 1. 内存使用情况分析 2. 内存泄漏(memory leak) 3. Valgrind使用 1. 内存使用情况分析 0x1: 系统总内存的分析 可以从proc目录下的meminfo文件了解到当前系统内存的使用情况汇总,其中可用的物理内存 = memfree + buffers + cached当memfree不够时,内核会通过回写机制(pdflush线程)把cached和buffered内存回写到后备存储器,从而释放相关内存供进程使用,或者通过手动方式显式释放cache内存:echo 3

Memory Leak Detection in Embedded Systems

One of the problems with developing embedded systems is the detection of memory leaks; I've found three tools that are useful for this. These tools are used to detect application program errors, not kernel memory leaks. Two of these tools (mtrace and

b.Jre Memory Leak Prevention Listener

本文我们来分析分析应用服务器的内存泄露的问题,看看Tomcat是如何应对这个问题的: 首先,来看看内存泄露这个词,内存对于java程序来说,即指JVM内存,而我们知道JVM的内存泄露是有很多种情况的: 一种情况,class泄露是perm区的内存,此种场景就是当应用服务器的类特别多的时候,perm区的容量也是由限度的,会撑爆: 另外一种就是,java对象太多,新生代,老年代等对象存储区太多,full GC都已经收拾不了这种残局: 上述的两种情况,最终都会导致OOM溢出: 而作为Tomcat来讲,如

Identify Memory Leaks in Visual CPP Applications(VLD内存泄漏检测工具)

原文地址:http://www.codeproject.com/Articles/1045847/Identify-Memory-Leaks-in-Visual-CPP-Applications 基于CPOL License Identify Memory Leaks in Visual CPP Applications Visual Leak Detector (VLD) is an easy to use memory leak detection system. The installat

vld(Visual Leak Detector) 内存泄露检测工具

初识Visual Leak Detector 灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题.当程序越来越复 杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题.内存泄漏是最常见的内存问题之一.内存泄漏如果不是很严重,在短时间内对程序不会有太大的 影响,这也使得内存泄漏问题有很强的隐蔽性,不容易被发现.然而不管内存泄漏多么轻微,当程序长时间运行时,其破坏力是惊人的,从性能下降到内存耗尽,甚 至会影响到其他程序的正常运行.另外内存问题的一个共同特点是,内存问题本身

How to detect and avoid memory and resources leaks in .NET applications

Despite what a lot of people believe, it's easy to introduce memory and resources leaks in .NET applications. The Garbage Collector, or GC for close friends, is not a magician who would completely relieve you from taking care of your memory and resou

The Introduction of Java Memory Leaks

One of the most significant advantages of Java is its memory management. You simply create objects and Java Garbage Collector takes care of allocating and freeing memory. However, the situation is not as simple as that, because memory leaks frequentl

vld(Visual Leak Detector) 内存泄露检测工具,Visual C++ 2008-2015

原文: https://vld.codeplex.com/ Visual Leak Detector 是一款专用于Visual C++的内存泄漏检测工具,它免费,开源,且鲁棒性高. VLD很容易使用: 1. 安装完vld后,只要告诉Visual C++哪里去找到它的头文件和库.(下载地址:https://vld.codeplex.com) 2. 然后您只要在你的c/c++工程中加上下面这一行代码,就可以使用vld了:#include <vld.h> 您的工程在Visual Studio deb