c++ Dynamic Memory (part 2)

Don‘t use get to initialize or assign another smart pointer.

The code that use the return from get can not delete the pointer

Although the compiler will not complain, it is an error to build another smart pointer to the pointer returned by get

shared_ptr<int> p(new int(42));    // reference count is 1
int *q = p.get();    // ok; but do not delete its pointer
{
    shared_ptr<int> (q);
}    // block ends. q is destroyed, and the memory to which q points is freed
int f = *p;    // undefined. The memory to which p points is freed. 

Using Our Own Deletion Code

void end_connection(connection *p)
{
    disconnection(*p);
}

void f(destination &d)
{
    connection c = conn(&d);
    shared_ptr<connection p(&c, end_connection);
    // use the connection
    // when f exists, even if by an exception, the connection resource will be properly closed
}

For unique_ptr

Call release() breaks the connection between a unique_ptr and the object it had been managing. Ofter the pointer returned by release() is used to initialized or assign another pointer

unique_ptr<int> p2(new int(42));
p2.release();    // WRONG! P2 will not free the memory, and we have lose the pointer
auto p = p2.release();    // OK, but we must remember to delete p

Backward compatibilities auto_ptr

Although auto_ptr is still part of the standard library, programs should use unique_ptr instead.

时间: 2024-10-21 16:08:22

c++ Dynamic Memory (part 2)的相关文章

c++ Dynamic Memory

1. make_shared<T>(args): return a shared_ptr dynamically allocated object of type T. Use args to initialize the object. shared_ptr<T> p(q): p is a copy of shared_ptr q. Increase the count in q. The pointer in q must be convertable to T. p = q:

Cpp Chapter 12: Classes and Dynamic Memory Allocation Part1

12.1 Dynamic memory and classes 12.1.1 A review example and static class members Now try implement a String class(a flawed one): // strngbad.h -- flawed string class definition #include <iostream> #ifndef STRNGBAD_H_INCLUDED #define STRNGBAD_H_INCLU

Cpp Chapter 12: Classes and Dynamic Memory Allocation Part2

12.3 Things to remember when using new in constructors ) If you use new in constructors, use delete in destructor. Their use should be compatible, pair new with delete and new [] with delete [] ) Multiple constructors should share the same way of new

CIT 593 Dynamic Memory

CIT 593 | Assignment: Dynamic Memory & File I/O | 1Setting up Codio for this HW:1) Open the Codio assignment via Coursera2) From the Codio File-Tree click on: lc4_memory.h and lc4_memory.cOverview:The goal of this HW is for you to write a program tha

[Paper翻译]Scalable Lock-Free Dynamic Memory Allocation

原文: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.87.3870&rep=rep1&type=pdf Abstract 动态内存分配器(malloc/free)在多线程环境下依靠互斥锁来保护共享数据的一致性.使用锁在性能,可用性,健壮性,程序灵活性方面有很多缺点.Lock-free的内存分配器能消除线程延迟或被杀死以及CPU的调度策略对程序的性能影响.这篇paper呈上了一个完整的无锁内存分配器.它的实现只使用被广泛支

C++ storage allocation + Dynamic memory allocation + setting limits + initializer list (1)

1. 对象的空间在括号开始就已经分配,但是构造在定义对象的时候才会实现,若跳过(譬如goto),到括号结束析构会发生错误,编译会通不过. 2.初始化 1 struct X { int i ; float f; char c;}; 2 3 - X x1 = { 1,2.2,'c'}; 4 X x2[3] = { {1,1.1,'a'},{2,2.2,'b'} }; 5 6 7 struct Y {float f; int i; Y(int a);} ; 8 9 Y y1[] = {Y(1),Y(2

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

《CS:APP》 chapter 9 Vitrual Memory 笔记

Vitrual Memory In order to manage memory more efficiently and with fewer errors, modern systems provide an abstraction of main memory known as virtual memory (VM). Virtual memory is an elegant interaction of hardware exceptions, hardware ad-dress tra

Memory Layout (Virtual address space of a C process)

Memory Layout (Virtual address space of a C process) 分类: C语言基础2012-12-06 23:16 2174人阅读 评论(0) 收藏 举报 found a good example to demostrate the memory layout and its stack info of a user-mode process, only that this example is for Linux. But it is still wo