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

#42-MODERN-C++-FOR-C-PROGRAMMERS-PART-5 #85

Merged

Conversation

kele1997
Copy link
Member

@kele1997 kele1997 commented Sep 7, 2018

(提交翻译请使用下面的模板,其他类型pr请删除)

closes #42

1.译者信息

(转载时的署名信息,有需要请补充)

伯乐在线id 新浪微博

2.疑问区

(可在此区域列举有疑问的语句,提示校对人员重点关注)

3.自查表

  • 确认pr的目标分支是trans分支
  • 确认pr不包含除译文以外的文件
  • 补充评论区closes关键字以关联对应issue
  • 通读文章,确保读者(非译者)能够理解文章内容(语言性,非技术性)
  • 通读文章,确保语句通顺(有疑问语句请在疑问区提出)
  • 符合排版规范要求

4.发布信息(译者无需填写)

发布链接:


@hanxiaomax

@ghost ghost assigned kele1997 Sep 7, 2018
@ghost ghost added the B3-Need review 完成翻译待校对,请至对应的pr认领校对并添加校对中标签 label Sep 7, 2018
@hanxiaomax hanxiaomax self-requested a review September 11, 2018 11:11
@hanxiaomax hanxiaomax added B4-Reviewing 文章正在校对中,完成后评论合入主线并添加C1 and removed B3-Need review 完成翻译待校对,请至对应的pr认领校对并添加校对中标签 labels Sep 11, 2018
@hanxiaomax
Copy link
Member

认领校对

## 内存管理

Memory is frequently the most important factor determining a program’s speed and reliability. CPUs these days tend to be tremendously faster than their attached RAM, so preventing needless copies and fragmented memory access may deliver whole orders of magnitude improvements in speed.
内存管理通常来说是决定一个程序运行速度和可靠性的重要因素。目前的 CPU 比起与它们相连的 RAM 速度快的多得多,因此如果能够防止不必要的拷贝和内存碎片访问,程序的运行速度将会带来巨大的提升。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

程序的运行速度将会带来巨大的提升 ---> xxx给xxx带来巨大的提升,这里不符合这种句式
建议直接:程序的运行速度将会有巨大的提升

在[第二部分](https://ds9a.nl/articles/posts/cpp-2)我们接触到了智能指针。我们也注意到内存泄露是所有项目的灾难,这大概也是用纯 C 语言编写代码最让人头疼的事情。我认识的 C (和 C++) 程序员都曾在解决隐蔽的内存泄露的问题上花过至少一个周的痛苦时间。

These problems are so large that most modern programming languages decided to incur a significant amount of overhead to implement garbage collection (GC). GC is amazing when it works, and especially lately, the overhead is now at least manageable. But as of 2018, all environments still struggle with hick-ups caused by GC runs, which always tend to happen exactly when you don’t want them to. And to be fair, this is a very difficult problem to solve, especially when many threads are involved.
这些问题太严重了以至于现代编程语言决定以牺牲大量开销为代价来实现垃圾回收(GC)。GC 在开始使用的时候非常神奇的,特别是刚刚应用的那段时间,开销还处于可控状态。但是在 2018 年,所有环境都在和 GC 运行造成的混乱做斗争,这种情况通常会在你想不到的时候发生。公平来说,这是个非常难解决的问题,特别是当涉及许多线程的时候。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

许多线程,翻译为多线程吧,不然没有英文原文的情况下,读起来有点像笔误

## 简介:std::unique_ptr

The overhead of generic reference counted pointer was well known when C++ took its initial standardized form. Back then, a quirky smart pointer called [std::auto_ptr](https://en.cppreference.com/w/cpp/memory/auto_ptr) was defined, but it turned out that within the C++ of 1998 it was not possible to create something useful. Making “the perfect smart pointer” required features that only became available in C++ 2011.
当 C++ 采用最初的标准化形式时,普通的引用计数指针的开销是非常出名的。那时,一个叫做 [`std::auto_ptr`](https://en.cppreference.com/w/cpp/memory/auto_ptr) 的古怪的智能指针被定义出来,但结果就是在 1998 年的 C++ 中,根本不可能创造出有用的东西。创造“完美的智能指针”所需要的特性直到 2011 年 C++ 才能提供。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但结果就是在 1998 年的 C++ 中,根本不可能创造出有用的东西。--> 但结果就是在 C++(98)中,我们根本不可能利用它创造出有用的东西。

>通常来说,我偏爱使用 `std::make_*` 这种形式来使用智能指针。对于 `std::shared_ptr` 来说,它把两次内存分配变成了一次,这在 CPU 周期和内存消耗上是一个巨大的胜利。

It should be noted that std::unique_ptr may be a smart pointer, but it is not a generic reference counted pointer. Or, to put it more precisely, there is always exactly one place that owns a std::unique_ptr. This is the magic of why there is no overhead: there is no reference count to store, it is always ‘1’.
应该指出的是 `std::unique_ptr` 可能也是一个智能指针,但它不是一个普通的引用计数指针。或者,更准确的说,应该有一个位置是属于 `std::unique_ptr` 的。 这就是为什么没有额外开销的原因:不需要保存引用计数器,因为引用计数永远是 ‘1’。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to put it more precisely, there is always exactly one place that owns a std::unique_ptr
这里应该是:这个指针总是指向某个对象(所以后面说不需要引用计数)

Copy link
Member

@hanxiaomax hanxiaomax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@hanxiaomax hanxiaomax merged commit 7415c24 into jobbole:trans Sep 18, 2018
@ghost ghost removed the B4-Reviewing 文章正在校对中,完成后评论合入主线并添加C1 label Sep 18, 2018
@hanxiaomax hanxiaomax added the C1-Finished 校对完成,待发布 label Oct 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C1-Finished 校对完成,待发布
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MODERN C++ FOR C PROGRAMMERS: PART 5
2 participants