第4章アルゴリズムの言語による実現:低水準プログラミング言語
TECHNOLOGY IN EVERYDAY LIFE日常生活の中の技術
C’s Application in the Early Stages of YouTubeYouTube初期のCの利用
C has a variety of integer types, such as
short, int, and long. A C programmer needs to decide on the most appropriate type for each piece of information in their program. Smaller types use less memory but can only store a narrower range of values. On a typical computer, the maximum short value is about 32 thousand and the maximum int value is about 2 billion. A good practice is to think critically about how large a particular value might become and pick the smallest data type that accommodates that range.英語のヒント
Cには、short、int、longなど、さまざまな整数型がある。Cのプログラマーは、プログラム内の各情報に最適な型を決める必要がある。小さい型は使用するメモリが少ないが、格納できる値の範囲も狭い。一般的なコンピューターでは、shortの最大値は約3万2千、intの最大値は約20億である。ある値がどれほど大きくなりうるかを慎重に考え、その範囲を収められる最小のデータ型を選ぶのがよい習慣である。
The YouTube programmers faced this issue when they implemented the view counter for YouTube videos. They had to think through: what is the maximum number of views that a video is likely to garner? Two billion seemed like a safe choice, so they chose
int.英語のヒント
YouTubeのプログラマーは、YouTube動画の視聴回数カウンターを実装するとき、この問題に直面した。考え抜く必要があったのは、「1本の動画が獲得しそうな視聴回数の最大値はいくらか」ということだった。20億なら安全な選択に思えたため、intを選んだ。
This decision turned out to be misguided. In 2014, the viral hit music video “Gangnam Style” by the Korean artist Psy accumulated more than two billion views, and the view counter broke. The
int variable storing the number of views of “Gangnam Style” overflowed and wrapped around to a negative number. This proved to be an embarrassment for YouTube, who had to quickly change their code to use long instead.英語のヒント
この判断は誤りだったとわかった。2014年、韓国のアーティストPsyによる、爆発的なヒットとなったミュージックビデオ「江南スタイル」が20億回を超える視聴回数を集め、カウンターが壊れた。「江南スタイル」の視聴回数を格納するint変数がオーバーフローし、値が一周して負数になった。これはYouTubeにとって恥ずかしい失敗となり、同社は代わりにlongを使うよう、急いでコードを変更しなければならなかった。
What is the most appropriate integer data type (
short, int, or long) for the following quantities?次の数量には、どの整数データ型(short、int、long)が最も適しているか。
1. The number of people on an airplane
1. 飛行機に乗っている人数
2. The number of people on Earth
2. 地球上の人口
3. The number of people in a household
3. 1世帯の人数
4. The number of dollars in a bank account
4. 銀行口座の金額(ドル単位)
High-level languages usually check array indices at runtime, which makes out-of-range bugs easy to identify and fix, but slows down array subscripts slightly. As a middle-level language, C does not check array indices. An array is a storage space where the elements are stored in contiguous memory cells. They are indexed from 0 (the first cell) to n−1 (last cell).
英語のヒント
高水準言語は通常、実行時に配列の添字を検査する。そのため、範囲外のバグを発見して修正しやすいが、配列の添字によるアクセスは少し遅くなる。中水準言語であるCは、配列の添字を検査しない。配列とは、要素が連続したメモリセルに格納される記憶領域である。添字は0(最初のセル)からn−1(最後のセル)まで付く。
In C, an invalid array subscript will access memory outside of the array variable. If the subscript is only out of range by a little bit, this will access nearby variables, which is a subtle bug that may go unnoticed. A segmentation fault (“segfault” for short) occurs if the subscript is very far out of range. When this occurs, it will access a memory address that is off-limits to the program, and your operating system will forcibly shut down the program in response. This kind of runtime error can be notoriously difficult to remedy. Out-of-bounds array subscripts are a common source of segmentation fault errors.
英語のヒント
Cでは、無効な配列添字を使うと、配列変数の外のメモリへアクセスする。添字が少しだけ範囲を外れている場合は、近くの変数へアクセスするため、気づかれないこともある微妙なバグになる。添字が範囲を大きく外れると、セグメンテーション違反(略してsegfault)が起こる。この場合、プログラムに立入りが許されないメモリアドレスへアクセスするため、オペレーティングシステムはそれに応じてプログラムを強制終了する。この種の実行時エラーは、修復が非常に難しいことで知られる。範囲外の配列添字は、セグメンテーション違反エラーのよくある原因である。
Every value in a program is stored at a specific memory address. A pointer is a value that contains a memory address. Technically, a pointer should contain the location of a valid data value. However, many memory locations do not contain valid data values, so it is possible to have an invalid pointer that does not hold a valid location. The pointee is the value that a pointer points at. A pointer is analogous to a street address such as “123 Main Street,” because it refers to a specific location. In that analogy, each building is a pointee. Usually, an address is valid and refers to a place you can visit. However, it is possible to have an invalid address that is not a place that can be visited; for example, if the building at that location was demolished.
英語のヒント
プログラム内の値は、それぞれ特定のメモリアドレスに格納される。ポインターとは、メモリアドレスを含む値である。厳密には、ポインターは有効なデータ値の場所を含むべきである。しかし、多くのメモリ位置には有効なデータ値がないので、有効な場所を保持しない無効なポインターもありうる。ポインティーとは、ポインターが指す値のことである。ポインターは特定の場所を指すため、「123 Main Street」のような番地に似ている。このたとえでは、個々の建物がポインティーに当たる。通常、住所は有効で、訪れることのできる場所を指す。しかし、たとえばその場所の建物が取り壊されていれば、訪れることのできる場所ではない無効な住所もありうる。
One of the differences between middle-level and high-level languages is that high-level languages either prohibit invalid pointers entirely, or provide mechanisms to handle them safely. As a middle-level language, C gives programmers the freedom to create null/invalid pointers, which can be helpful when writing code that interfaces with hardware devices. Since all hardware devices do not support the same functionality, the support of individual features by a given device may be indicated as a null/uninitialized pointer, which is fine as long as the program checks for un-initialized pointers to determine if a given functionality is available. However, in general, the freedom of using null/invalid pointers comes with a responsibility to ensure that pointers are always used properly. This has proven to be difficult; invalid pointers are a common source of bugs in C programs.
英語のヒント
中水準言語と高水準言語の違いの1つは、高水準言語では無効なポインターを全面的に禁止するか、安全に扱う仕組みを備えることである。中水準言語であるCは、ヌルポインターや無効なポインターを作る自由をプログラマーに与える。これは、ハードウェア装置とやり取りするコードを書くときに役立つ場合がある。すべてのハードウェア装置が同じ機能に対応するわけではないので、特定の装置が個々の機能に対応するかどうかを、ヌルポインターや未初期化ポインターで示すことがある。プログラムが未初期化ポインターを調べて、ある機能が利用できるか判断する限り、これは問題ない。しかし一般に、ヌルポインターや無効なポインターを使う自由には、ポインターを常に適切に使うようにする責任が伴う。これを守るのは難しいとわかっており、無効なポインターはCプログラムのバグのよくある原因である。
In C, the programmer is responsible for making sure that character arrays are actually big enough to fit strings, and that strings include the null terminator character. A character array is a string of characters sometimes terminated using a null. An example might be something like:
char *arr= "string\0". Overlooking either of these results in bugs. This is a prime example of how middle-level languages such as C expect programmers to deal with more details than do high-level languages.英語のヒント
Cでは、文字配列が文字列を収めるのに実際に十分な大きさであることと、文字列がヌル終端文字を含むことを確認する責任は、プログラマーにある。文字配列は文字を並べたもので、ヌルを使って終端する場合がある。たとえば、char *arr= "string\0"のような形になる。このどちらかを見落とすとバグになる。これは、Cのような中水準言語では、高水準言語より多くの細部をプログラマーが扱う必要があることの、格好の例である。