Skip to content

Commit

Permalink
ch12 ex12.13
Browse files Browse the repository at this point in the history
  • Loading branch information
huangmingchuan committed Jan 15, 2016
1 parent f85f5ff commit 001b19a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cpp_Primer_Answer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ch12\exercise12_2.cpp" />
<ClInclude Include="ch12\exercise12_2.h" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ch12\exercise12_2.h" />
<ClCompile Include="ch12\exercise12_7.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{EAEE1AD0-0D58-421C-BAF5-604CEA494D9C}</ProjectGuid>
Expand Down
10 changes: 5 additions & 5 deletions Cpp_Primer_Answer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ch12\exercise12_2.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ch12\exercise12_2.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ch12\exercise12_7.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>
19 changes: 17 additions & 2 deletions ch12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ StrBlob b1;
构造函数不是 explicit 的,意味着可以从 initializer_list 隐式转换为 StrBlob。在 StrBlob 对象中,只有一个数据成员 data,而 StrBlob 对象本身的含义,也是一个**管理字符串的序列**。因此,从 initializer_list 到 StrBlob 的转换,在逻辑上是可行的。而这个设计策略的缺点,可能在某些地方我们确实需要 initializer_list,而编译器仍会将之转换为 StrBlob。

## 练习12.6
## [练习12.6](exercise12_6.cpp)

> 编写函数,返回一个动态分配的 int 的vector。将此vector 传递给另一个函数,这个函数读取标准输入,将读入的值保存在 vector 元素中。再将vector传递给另一个函数,打印读入的值。记得在恰当的时刻delete vector。
## 练习12.7
## [练习12.7](exercise12_7.cpp)

> 重做上一题,这次使用 shared_ptr 而不是内置指针。
Expand All @@ -53,6 +53,8 @@ bool b() {
}
```

有错误。没有释放指针 p 。

## 练习12.9

> 解释下面代码执行的结果。
Expand All @@ -63,6 +65,8 @@ auto q2 = make_shared<int>(42), r2 = make_shared<int>(100);
r2 = q2;
```

r 和 q 指向 42,而之前 r 指向的 100 的内存空间并没有被释放,因此会发生内存泄漏。r2 和 q2 都是智能指针,当对象空间不被引用的时候会自动释放。

## 练习12.10

> 下面的代码调用了第413页中定义的process 函数,解释此调用是否正确。如果不正确,应如何修改?
Expand All @@ -71,13 +75,17 @@ shared_ptr<int> p(new int(42));
process(shared_ptr<int>(p));
```
正确。`shared_ptr<int>(p)` 会创建一个临时的智能指针,这个智能指针与 p 引用同一个对象,此时引用计数为 2。当表达式结束时,临时的智能指针被销毁,此时引用计数为 1。
## 练习12.11
> 如果我们像下面这样调用 process,会发生什么?
```cpp
process(shared_ptr<int>(p.get()));
```

这样会创建一个新的智能指针,它的引用计数为 1,这个智能指针所指向的空间与 p 相同。在表达式结束后,这个临时智能指针会被销毁,引用计数为 0,所指向的内存空间也会被释放。而导致 p 所指向的空间被释放,使得 p 成为一个空悬指针。

## 练习12.12

> p 和 sp 的定义如下,对于接下来的对 process 的每个调用,如果合法,解释它做了什么,如果不合法,解释错误原因:
Expand All @@ -90,6 +98,11 @@ auto sp = make_shared<int>();
(d) process(shared_ptr<int>(p));
```

* (a) 合法。将sp 拷贝给 process函数的形参,在函数里面引用计数为 2,函数结束后引用计数为 1。
* (b) 不合法。不能从内置指针隐式转换为智能指针。
* (c) 不合法。不能从内置指针隐式转换为智能指针。
* (d) 合法。但是智能指针和内置指针一起使用可能会出现问题,在表达式结束后智能指针会被销毁,它所指向的对象也被释放。而此时内置指针 p 依旧指向该内存空间。之后对内置指针 p 的操作可能会引发错误。

## 练习12.13

> 如果执行下面的代码,会发生什么?
Expand All @@ -99,6 +112,8 @@ auto p = sp.get();
delete p;
```

智能指针 sp 所指向空间已经被释放,再对 sp 进行操作会出现错误。

## 练习12.14

> 编写你自己版本的用 shared_ptr 管理 connection 的函数。
Expand Down
35 changes: 35 additions & 0 deletions ch12/exercise12_6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>

using std::vector;

vector<int>* alloc_vector()
{
return new vector<int>();
}

void assign_vector(vector<int>* p)
{
int i;
while (std::cin >> i)
{
p->push_back(i);
}
}

void print_vector(vector<int>* p)
{
for (auto i : *p)
{
std::cout << i << std::endl;
}
}

int main()
{
auto p = alloc_vector();
assign_vector(p);
print_vector(p);
delete p;
return 0;
}
35 changes: 35 additions & 0 deletions ch12/exercise12_7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <vector>
#include <memory>

using std::vector;

std::shared_ptr<vector<int>> alloc_vector()
{
return std::make_shared<vector<int>>();
}

void assign_vector(std::shared_ptr<vector<int>> p)
{
int i;
while (std::cin >> i)
{
p->push_back(i);
}
}

void print_vector(std::shared_ptr<vector<int>> p)
{
for (auto i : *p)
{
std::cout << i << std::endl;
}
}

int main()
{
auto p = alloc_vector();
assign_vector(p);
print_vector(p);
return 0;
}

0 comments on commit 001b19a

Please sign in to comment.