Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update 练习12.17 #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions ch12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,14 @@ typedef unique_ptr<int> IntP;
(e) IntP p4(new int(2048));
(f) IntP p5(p2.get());
```

注:“合法”意味着编译可以通过,但不代表不会产生错误!
* (a) 不合法。在定义一个 unique_ptr 时,需要将其绑定到一个new 返回的指针上。
* (b) 合法。但是可能会有后续的程序错误。当 p1 被释放时,p1 所指向的对象也被释放,所以导致 pi 成为一个空悬指针。
编译时报错: invalid conversion from 'int' to 'std::unique_ptr<int>::pointer { aka int* }' [-fpermissive]
* (b) 合法。但是可能会有后续的程序错误。当 p1 被释放时,其实际所指向的对象 —— ix 也会被释放,但是 ix 并非通过 new 创建,所以此时操作系统会报错。
* (c) 合法。但是也可能会使得 pi2 成为空悬指针。
* (d) 不合法。当 p3 被销毁时,它试图释放一个栈空间的对象。
* (d) 合法。但是当 p3 被销毁时,它试图释放一个栈空间的对象。
* (e) 合法。
* (f) 不合法。p5 和 p2 指向同一个对象,当 p5 和 p2 被销毁时,会使得同一个指针被释放两次。
* (f) 合法。但是p5 和 p2 指向同一个对象,当 p5 和 p2 被销毁时,会使得同一个指针被释放两次。

## 练习12.18

Expand Down