質問

GCC4.4.1,C99

を使用してい size_t, は、 size_tunsigned int.しかし、それはそる場合には32ビットまたは64ビット.

かするとのことです。 size_t 店舗のサイズのバッファです。

とは思わないそれらの携帯を使用する場合は全述べた。

のためだけではなく、今、 size_t どちら32または64ビット.う状況が最も深刻な問題なのか。

役に立ちましたか?

解決

size_t 実装中のオブジェクトのバイト数を保持できるように保証されています。それが戻りタイプの理由です sizeofsize_t.

そう、それはポータブルです。

他のヒント

他の人が言ったように、 size_t の結果を保存するのに正しく、完全に受け入れられます sizeof() または、バイト内の表現可能なオブジェクトのサイズ。気をつけなければならないのは次のとおりです。

  1. size_t と同じサイズです いくつかの署名されていない整数タイプ. 。必ずしも最大の署名されていない整数型と同じバイト数ではありません。 unsigned int, unsigned long, 、など
  2. sizeof(size_t) 実装定義のバイト数です memcpy'それを入力するか、以外の整数型に割り当てる uintmax_t 悪い考えです。サイズが等しいか、それよりも小さいと仮定しても安全であるかどうかさえわかりません uintmax_t.
  3. 書く size_t バイナリファイルへの値とそれを読み戻します size_t 別のプロセスで、別のマシンで、または異なるコンパイラオプションをコンパイルしたものによって、健康に危険になる可能性があります。
  4. 送信 size_t ネットワーク全体で価値を獲得し、 sizeof(size_t) 反対側のバッファーはかなり安全ではありません。

これらはすべて、他の整数タイプの標準的な問題です unsigned char. 。そう size_t 他の整数タイプと同じようにポータブルです。

この機能の一部を利用size_tはssize_tのためのバッファーを使用している場合、malloc()、またはread().持ち運びに便利な使用SIZE_MAX,SSIZE_MAX,sizeof(type-イン-あなたのバッファ)の%zdは%ずprintf().

また、OFF_TとPTRDIFF_T / SSIZE_Tもあります。これは、同じ方法でアーキテクチャ間で異なります。

それらを正しく使用する場合、それらはアーキテクチャ全体でポータブルです。 32ビットシステムでは、それらはすべて32ビット幅になりますが、64ビットシステムではすべて64ビット幅になります。これはあなたが望むものです - バッファーのサイズは、32ビットシステム上の32ビットSIZE_Tよりも大きくすることはできませんが、64ビットシステムでははるかに大きくなる可能性があります。

INT、ロング、または他のものを使用しないでください。他のものとは別に、長いサイズはプラットフォームによって異なります(ほとんどの32ビットシステムでは32ビット、64ビットUNIXシステムで64ビット、64ビットウィンドウで32ビット)。

It is hard to figure out what you mean by "portable" in this case. The term "portable" allows multiple sinificantly different interpretations.

Type size_t has a very specific purpose. It can hold the size of any object in the given implementation. I.e. it is a type that can always receive the result of sizeof() operator. Type size_t has no other purpose, and within its intended application it is 100% portable, as portable as anything can be.

What kind of "portable" you are asking about is, once again, not clear.

You should not assume that size_t is an unsigned int (see this answer), but I think it is has the same range on both architectures.

Depends on what you are using size_t for.

If you use it to determine the size of a memory buffer it will be safe, since size_t is large enough to address the whole memory of any computer. So if the memory buffer is larger than that, you have a problem anyway.

On the other hand, if you use it as a generic unsigned integer to count the number of stars in the universe for example, you might have a problem on 32-bit system (not sure about 64-bit systems).

The only real serious problem with this is trying to access a rather large array or a large number for size_t.

Just like just a regular "int" might be enough on a 64-bit, but could cause a crash on a 32-bit because its too large for a int on a 32-bit system.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top