在 C++ Primer 5th edition shared_ptr<int> 中发现错误 [英] Error spotted in C++ Primer 5th edition shared_ptr<int>

查看:45
本文介绍了在 C++ Primer 5th edition shared_ptr<int> 中发现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 C++ 入门第 5 版,我想我在 shared_ptr 部分发现了一个错误.首先,我正在编写代码和他们给出的解释.然后我会写出我认为的错误以及我认为实际发生的事情.代码如下:

Hi i am reading C++ primer 5th edition and i think i have spotted one error under the section shared_ptr. First i am writing the code and the explanation that they have given. Then i will write what i think is the error and what i think is actually happening. The code is as follows:

shared_ptr<int> p(new int(42));// reference count is 1
int *q = p.get();// ok: but don't use q in any way that might delete its pointer
{//new block started
    shared_ptr<int>(q);
}// block ends, q is destroyed, and the memory to which q points is freed
int foo = *p;// undefined; the memory to which p points was freed

他们给出的解释如下:

在这种情况下,p 和 q 都指向同一个内存.因为它们是彼此独立创建的,所以每个引用计数都是 1.当定义 q 的块结束时,q 被销毁.销毁 q 会释放 q 指向的内存.这使得 p 成为一个悬空指针,这意味着当我们尝试使用 p 时会发生什么是未定义的.此外,当 p 被销毁时,指向该内存的指针将再次被删除.

In this case, both p and q point to the same memory. Because they were created independently from each other, each has a reference count of 1. When the block in which q was defined ends, q is destroyed. Destroying q frees the memory to which q points. That makes p into a dangling pointer, meaning that what happens when we attempt to use p is undefined. Moreover, when p is destroyed, the pointer to that memory will be deleted a second time.

现在我认为错误是语句当定义 q 的块结束时,q 被销毁.销毁 q 释放 q 指向的内存.";以及他们给出的为什么 p 是悬空指针的推理是错误的.下面是我为什么 p 是悬空指针以及为什么第一个引用的语句是错误的推理.

Now i think the error is the statement "When the block in which q was defined ends, q is destroyed.Destroying q frees the memory to which q points." and also the reasoning they gave behind why p is a dangling pointer is faulty. Below is my reasoning why p is a dangling pointer and why first quoted statement is an error.

  1. 当定义 q 的块结束时,q 被销毁.但是 q 指向的内存并没有被释放,因为 q 是一个内置指针而不是一个 shared_ptr.除非我们明确写入 delete q,否则不会释放相应的内存.
  2. 现在,在新块中,我们使用 q 创建了一个临时的 shared_ptr.但是这个临时文件是独立于 p 的.所以当这个内部块结束时,临时块被销毁,因此内存被释放.但是请注意 p 仍然指向已释放的相同内存.所以 p 现在是一个悬空指针,并且在语句 int foo=*p 中使用 p 是未定义的.
  1. When the block in which q was defined ends, q is destroyed. But the memory to which q points is not freed since q is a builtin pointer and not a shared_ptr. And unless we explicitly write delete q the corresponding memory will not be freed.
  2. Now, inside the new block we have created a temporary shared_ptr using q. But this temporary is independent of p. and so when this inner block ends the temporary is destroyed and hence the memory is freed. But note that p still point to the same memory which has been freed. So p is now a dangling pointer and using p in the statement int foo=*p is undefined.

我认为这是对为什么 p 是悬空指针的正确解释,也是应该存在的更正.有人可以确认这是正确的还是我做错了什么?

I think this is the correct explanation of why p is a dangling pointer and also the correction that should be there. Can someone confirm if this is right or am i doing something wrong?

推荐答案

正如您正确指出的那样,代码中的文本描述和注释都与代码不符.它们更符合这样的代码:

As you rightfully pointed out, the text description, and the comments in the code both do not fit the code. They are more in line with code like this:

shared_ptr<int> p(new int(42));// reference count is 1
{//new block started
    shared_ptr<int> q(p.get());
}// block ends, q is destroyed, and the memory to which q points is freed
int foo = *p;// undefined; the memory to which p points was freed

如果让我猜的话,我会说这就是示例最初的样子,然后有人决定引入原始指针,却没有意识到它也需要更改注释和文本.

If I were to guess, I would say this is how the example first looked like, and then somebody decided to introduce the raw pointer without realising it would also need changes to the comments and text.

这篇关于在 C++ Primer 5th edition shared_ptr&lt;int&gt; 中发现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆