14

intC標準は、可能なすべての配列サイズを格納できることを保証します。少なくとも、それは私が§6.5.2.1のサブセクション1(配列の添え字制約)を読んで理解していることです:

式の1つは「オブジェクト型へのポインタ」型であり、もう1つの式は整数型であり、結果は「型」型である必要があります。

int配列の添え字としてsを使用するので、なぜ配列size_tのサイズを決定するために使用することになっているのでしょうか。

なぜ十分なときにstrlen()戻るのですか?size_tint

4

5 に答える 5

25

int「整数型」という用語は、たとえば、を意味するのではなくcharshort整数型です。

を使用して配列に添え字を付けることができるからといってint、必ずしもすべての可能な配列要素に到達できるとは限りません。

より具体的には、size_tvs 。についてint、1つの例はint、16ビットタイプとsize_t32ビットタイプ(または、今日の64ビットプラットフォームでより一般的な32ビットintと64ビットの違い)である可能性があるプラットフォームです。size_t

于 2011-05-14T19:54:57.947 に答える
6

integer type is not necessarily an "int". "long long" is an integer type too, as is "size_t".

Arrays can be larger than 2GB. This property is quite handy for those who write memory hungry programs, e.g DBMS with big buffer pools, application servers with big memory caches etc. Arrays bigger than 2GB/4GB is the whole point of 64 bit computing :)

size_t for strlen(), at least sounds compatible with how C standard handles arrays, whether it makes practical sense or not, or whether somebody have seen strings that large, is another question.

于 2011-05-14T20:02:37.500 に答える
2

Firstly, what you quoted from the standard does not make any references to type int specifically. And no, int is not guaranteed to be sufficient to store the size of any object (including arrays) in C.

Secondly, C language does not really have "array subscriptions" specifically. The array subscription is implemented through pointer arithmetic. And the integral operand in pointer arithmetics has ptrdiff_t type. Not size_t, not int, but ptrdiff_t. It is a signed type, BTW, meaning that the value can be negative.

Thirdly, the purpose of size_t is to store the size of any object in the program (i.e. to store the result of sizeof). It is not immediately intended to be used as an array index. It just happens to work as an array index since it is guaranteed that it is always large enough to index any array. However, from an abstract point of view, "array" is a specific kind of "container" and there are other kinds of containers out there (lists-based ones, tree-based ones and so on). In generic case size_t is not sufficient to store the size of any container, which in generic case makes it a questionable choice for array indexing as well. (strlen, on the other hand, is a function that works with arrays specifically, which makes size_t appropriate there.)

于 2011-05-14T20:00:34.220 に答える
0

C標準が作成されたとき、マシンは16ビットの「int」型を持ち、65535バイトを超える単一のオブジェクトを処理できませんが、それでも32767バイトを超えるオブジェクトを処理できるのが一般的でした。unsigned intの算術演算は、そのようなオブジェクトの最大サイズを処理するのに十分な大きさですが、signed intの算術演算はそうで​​はないため、size_tは、「長い」計算を使用せずにそのようなオブジェクトに対応できるようにunsignedとして定義されました。

最大許容オブジェクトサイズがINT_MAXとUINT_MAXの間にあるマシンでは、そのようなオブジェクトの開始と終了へのポインターの差が大きすぎて「int」に収まらない場合があります。標準では、実装がそれをどのように処理するかについての要件はありませんが、一般的なアプローチは、SとEがchar [49152]の開始と終了へのポインターである場合に、整数とポインターのラップアラウンド動作を定義することです。 ESがINT_MAXを超えたとしても、Sに追加されると、Eを生成する値が生成されます。

現在、size_tが符号なし型であり(2GBを超えるオブジェクトを必要とするコードは、他の理由で64ビットポインターを使用する必要があるため)、オブジェクトサイズを含む多くの種類の比較が動作するという事実には、実際の利点はほとんどありません。直感に反しますが、sizeof式が符号なし型を生成するという事実は十分に定着しているため、変更される可能性はほとんどありません。

于 2016-05-06T19:25:45.093 に答える
-3

size_t is a typedef of unsigned integer (such as int or long).

In some 64bit platforms, int can be 32bit, while size_t can be 64bit.

It is used as a more standard way for size.

于 2011-05-14T19:59:33.987 に答える