727

基本的な C++ 型のサイズに関する詳細情報を探しています。アーキテクチャ (16 ビット、32 ビット、64 ビット) とコンパイラに依存することはわかっています。

しかし、C++ の標準はありますか?

32 ビット アーキテクチャで Visual Studio 2008 を使用しています。ここに私が得るものがあります:

char  : 1 byte
short : 2 bytes
int   : 4 bytes
long  : 4 bytes
float : 4 bytes
double: 8 bytes

charさまざまなアーキテクチャやコンパイラでの、shortintlong、(および私が思いもよらなかった他の型)doubleのサイズを示す信頼できる情報を見つけようとしましたが、あまり成功しませんでした。float

4

24 に答える 24

90

実際には、そのようなことはありません。std::size_t多くの場合、現在のアーキテクチャでは符号なしのネイティブ整数サイズを表すことが期待できます。つまり、16ビット、32ビット、または64ビットですが、この回答へのコメントで指摘されているように、常にそうとは限りません。

他のすべての組み込み型に関する限り、実際にはコンパイラに依存します。以下は、最新の C++ 標準の現在のワーキング ドラフトからの 2 つの抜粋です。

標準の符号付き整数型には、signed char、short int、int、long int、long long int の 5 つがあります。このリストでは、各タイプは、少なくともリスト内の前のタイプと同じ量のストレージを提供します。

標準の符号付き整数型のそれぞれに対して、対応する (ただし異なる) 標準の符号なし整数型が存在します: unsigned char、unsigned short int、unsigned int、unsigned long int、および unsigned long long int で、それぞれ同じ量を占有します。ストレージと同じ配置要件があります。

必要に応じて、これらの基本型のサイズを静的に (コンパイル時に) アサートできます。sizeof 仮定が変更された場合、コードの移植について考えるよう人々に警告します。

于 2009-02-26T08:02:56.633 に答える
84

標準あり。

C90 標準では、

sizeof(short) <= sizeof(int) <= sizeof(long)

C99 標準では、

sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)

C99の仕様はこちら。22 ページには、さまざまな整数型のサイズが詳しく説明されています。

Windows プラットフォームの int 型のサイズ (ビット) は次のとおりです。

Type           C99 Minimum     Windows 32bit
char           8               8
short          16              16
int            16              32
long           32              32
long long      64              64

移植性に関心がある場合、または型の名前にサイズを反映させたい場合は<inttypes.h>、次のマクロが利用可能なheader を確認できます。

int8_t
int16_t
int32_t
int64_t

int8_t8ビットint16_t保証、16ビット保証など。

于 2009-03-30T14:49:54.030 に答える
39

固定サイズの型が必要な場合は、stdint.hで定義されている uint32_t (符号なし整数 32 ビット) などの型を使用します。それらはC99で指定されています。

于 2009-02-26T08:18:12.260 に答える
36

更新:C ++ 11により、TR1のタイプが正式に標準に組み込まれました。

  • long long int
  • unsigned long long int

そして、からの「サイズ」タイプ<cstdint>

  • int8_t
  • int16_t
  • int32_t
  • int64_t
  • (および署名されていない対応物)。

さらに、次のようになります。

  • int_least8_t
  • int_least16_t
  • int_least32_t
  • int_least64_t
  • 加えて、署名されていない対応物。

これらのタイプは、少なくとも指定されたビット数を持つ最小の整数タイプを表します。同様に、少なくとも指定されたビット数を持つ「最速」の整数型があります。

  • int_fast8_t
  • int_fast16_t
  • int_fast32_t
  • int_fast64_t
  • さらに、署名されていないバージョン。

「速い」とは、どちらかといえば、実装次第です。また、すべての目的で最速である必要はありません。

于 2009-02-26T19:32:40.530 に答える
18

C ++標準では、次のように記述されています。

3.9.1、§2:

符号付き整数型には、「signed char」、「short int」、「int」、「long int」、および「longlongint」の5つがあります。このリストでは、各タイプは、リスト内でその前にあるものと少なくとも同じ量のストレージを提供します。プレーンintは、実行環境のアーキテクチャによって提案される自然なサイズを持っています(44)。他の符号付き整数タイプは、特別なニーズを満たすために提供されています。

(44)つまり、ヘッダーで定義されているように、INT_MINからINT_MAXの範囲の値を含むのに十分な大きさ <climits>です。

結論:それはあなたが取り組んでいるアーキテクチャに依存します。その他の仮定は誤りです。

于 2010-09-01T13:41:13.873 に答える
12

いいえ、活字サイズの基準はありません。標準では次のことのみが必要です。

sizeof(short int) <= sizeof(int) <= sizeof(long int)

固定サイズの変数が必要な場合にできる最善の方法は、次のようなマクロを使用することです。

#ifdef SYSTEM_X
  #define WORD int
#else
  #define WORD long int
#endif

次に、WORD を使用して変数を定義できます。私はこれが好きというわけではありませんが、これが最も移植性の高い方法です。

于 2009-02-26T08:07:09.040 に答える
10

独自の「標準」を作成できるように、型の同義語を定義することが許可されています。

sizeof(int) == 4 のマシンでは、次のように定義できます。

typedef int int32;

int32 i;
int32 j;
...

そのため、実際に long int のサイズが 4 である別のマシンにコードを転送する場合、int の単一オカレンスを再定義するだけで済みます。

typedef long int int32;

int32 i;
int32 j;
...
于 2012-07-14T16:01:46.560 に答える
7

標準があり、さまざまな標準文書 (ISO、ANSI など) で指定されています。

ウィキペディアには、さまざまな型とそれらが格納できる最大値を説明する素晴らしいページがあります: Integer in Computer Science.

ただし、標準の C++ コンパイラを使用しても、次のコード スニペットを使用して比較的簡単に見つけることができます。

#include <iostream>
#include <limits>


int main() {
    // Change the template parameter to the various different types.
    std::cout << std::numeric_limits<int>::max() << std::endl;
}

std::numeric_limitsのドキュメントはRoguewaveにあります。さまざまな制限を見つけるために呼び出すことができる他のコマンドが多数含まれています。これは、std::streamsize など、サイズを伝える任意の型で使用できます。

ジョンの回答には、保持が保証されているため、最良の説明が含まれています。使用しているプラ​​ットフォームに関係なく、各型に含まれなければならないビット数について詳しく説明している別の優れたページがあります:標準で定義されているint typesです。

これが役立つことを願っています!

于 2009-02-26T08:06:35.843 に答える
6

さまざまなアーキテクチャやさまざまなコンパイラの組み込み型に関しては、コンパイラを使用してアーキテクチャで次のコードを実行するだけで、出力を確認できます。以下は、Ubuntu 13.04 (Raring Ringtail) 64 ビット g++4.7.3 の出力を示しています。また、以下の回答に注意してください。これが、出力がそのように順序付けられている理由です。

「標準の符号付き整数型には、signed char、short int、int、long int、long long int の 5 種類があります。このリストでは、各型は少なくともリストの前にあるものと同じ量のストレージを提供します。」

#include <iostream>

int main ( int argc, char * argv[] )
{
  std::cout<< "size of char: " << sizeof (char) << std::endl;
  std::cout<< "size of short: " << sizeof (short) << std::endl;
  std::cout<< "size of int: " << sizeof (int) << std::endl;
  std::cout<< "size of long: " << sizeof (long) << std::endl;
  std::cout<< "size of long long: " << sizeof (long long) << std::endl;

  std::cout<< "size of float: " << sizeof (float) << std::endl;
  std::cout<< "size of double: " << sizeof (double) << std::endl;

  std::cout<< "size of pointer: " << sizeof (int *) << std::endl;
}


size of char: 1
size of short: 2
size of int: 4
size of long: 8
size of long long: 8
size of float: 4
size of double: 8
size of pointer: 8
于 2013-08-08T23:47:45.473 に答える
5

次を使用できます。

cout << "size of datatype = " << sizeof(datatype) << endl;

datatype = intlong intなど。入力したデータ型のサイズを確認できます。

于 2011-06-14T06:19:18.543 に答える
3

前述のように、サイズは現在のアーキテクチャを反映する必要があります。limits.h現在のコンパイラがどのように処理しているかを確認したい場合は、ピークを取ることができます。

于 2009-02-26T08:01:31.893 に答える
2

他の人が答えたように、「標準」はすべて詳細のほとんどを「実装定義」のままにし、型「char」が少なくとも「char_bis」幅であり、「char <= short <= int <= long < = long long" (float と double は IEEE 浮動小数点標準とほぼ一致しており、long double は通常 double と同じですが、最新の実装ではより大きくなる可能性があります)。

非常に具体的で正確な値を持たない理由の 1 つは、C/C++ のような言語が多数のハードウェア プラットフォームに移植できるように設計されているためです。または7ビット、または平均的なホームコンピューターユーザーがさらされている「8/16/32/64ビット」コンピューター以外の値です。(ここでのワード サイズは、システムが通常動作するビット幅を意味します。繰り返しますが、ホーム コンピュータのユーザーが期待するように、常に 8 ビットであるとは限りません。)

特定のビット数のオブジェクト (整数値を表す一連のビットという意味で) が本当に必要な場合、ほとんどのコンパイラにはそれを指定する方法があります。しかし、それは通常、ame 社が作成したコンパイラ間であっても、異なるプラットフォームに対して移植可能ではありません。一部の標準とプラクティス (特に limits.h など) は十分に一般的であるため、ほとんどのコンパイラは、特定の範囲の値に最適な型を決定することをサポートしていますが、使用されるビット数はサポートしていません。(つまり、0 から 127 の間の値を保持する必要があることがわかっている場合、コンパイラが 8 ビットの「int8」型をサポートしていると判断できます。これは、必要な範囲全体を保持するのに十分な大きさですが、 7ビットに完全に一致する「int7」タイプ。)

注: 多くの Un*x ソース パッケージでは、"./configure" スクリプトが使用されていました。このスクリプトは、コンパイラ/システムの機能を調べて、適切な Makefile と config.h を出力します。これらのスクリプトのいくつかを調べて、それらがどのように機能するか、どのようにコミラー/システム機能を調査するかを確認し、その先導に従ってください。

于 2013-05-27T07:04:36.367 に答える
2

純粋な C++ ソリューションに興味がある場合は、テンプレートと C++ 標準コードのみを使用して、ビット サイズに基づいてコンパイル時に型を定義しました。これにより、ソリューションはコンパイラ間で移植可能になります。

背後にある考え方は非常に単純です: タイプ char、int、short、long、long long (signed および unsigned バージョン) を含むリストを作成し、リストをスキャンし、numeric_limits テンプレートを使用して指定されたサイズのタイプを選択します。

このヘッダーを含めると、stdtype::int8、stdtype::int16、stdtype::int32、stdtype::int64、stdtype::uint8、stdtype::uint16、stdtype::uint32、stdtype::uint64 の 8 つの型が得られます。

一部の型を表すことができない場合、そのヘッダーで宣言されている stdtype::null_type に評価されます。

以下のコードは保証なしで提供されます。再確認してください。
私はメタプログラミングも初めてです。このコードを自由に編集して修正してください。
DevC++ でテスト済み (gcc バージョンは 3.5 前後)

#include <limits>

namespace stdtype
{
    using namespace std;


    /*
     * THIS IS THE CLASS USED TO SEMANTICALLY SPECIFY A NULL TYPE.
     * YOU CAN USE WHATEVER YOU WANT AND EVEN DRIVE A COMPILE ERROR IF IT IS 
     * DECLARED/USED.
     *
     * PLEASE NOTE that C++ std define sizeof of an empty class to be 1.
     */
    class null_type{};

    /*
     *  Template for creating lists of types
     *
     *  T is type to hold
     *  S is the next type_list<T,S> type
     *
     *  Example:
     *   Creating a list with type int and char: 
     *      typedef type_list<int, type_list<char> > test;
     *      test::value         //int
     *      test::next::value   //char
     */
    template <typename T, typename S> struct type_list
    {
        typedef T value;
        typedef S next;         

    };




    /*
     * Declaration of template struct for selecting a type from the list
     */
    template <typename list, int b, int ctl> struct select_type;


    /*
     * Find a type with specified "b" bit in list "list"
     *
     * 
     */
    template <typename list, int b> struct find_type
    {   
        private:
            //Handy name for the type at the head of the list
            typedef typename list::value cur_type;

            //Number of bits of the type at the head
            //CHANGE THIS (compile time) exp TO USE ANOTHER TYPE LEN COMPUTING
            enum {cur_type_bits = numeric_limits<cur_type>::digits};

        public:
            //Select the type at the head if b == cur_type_bits else
            //select_type call find_type with list::next
            typedef  typename select_type<list, b, cur_type_bits>::type type;
    };

    /*
     * This is the specialization for empty list, return the null_type
     * OVVERRIDE this struct to ADD CUSTOM BEHAVIOR for the TYPE NOT FOUND case
     * (ie search for type with 17 bits on common archs)
     */
    template <int b> struct find_type<null_type, b>
    {   
        typedef null_type type;

    };


    /*
     * Primary template for selecting the type at the head of the list if
     * it matches the requested bits (b == ctl)
     *
     * If b == ctl the partial specified templated is evaluated so here we have
     * b != ctl. We call find_type on the next element of the list
     */
    template <typename list, int b, int ctl> struct select_type
    {   
            typedef  typename find_type<typename list::next, b>::type type; 
    };

    /*
     * This partial specified templated is used to select top type of a list
     * it is called by find_type with the list of value (consumed at each call)
     * the bits requested (b) and the current type (top type) length in bits
     *
     * We specialice the b == ctl case
     */
    template <typename list, int b> struct select_type<list, b, b>
    {
            typedef typename list::value type;
    };


    /*
     * These are the types list, to avoid possible ambiguity (some weird archs)
     * we kept signed and unsigned separated
     */

    #define UNSIGNED_TYPES type_list<unsigned char,         \
        type_list<unsigned short,                           \
        type_list<unsigned int,                             \
        type_list<unsigned long,                            \
        type_list<unsigned long long, null_type> > > > >

    #define SIGNED_TYPES type_list<signed char,         \
        type_list<signed short,                         \
        type_list<signed int,                           \
        type_list<signed long,                          \
        type_list<signed long long, null_type> > > > >



    /*
     * These are acutally typedef used in programs.
     * 
     * Nomenclature is [u]intN where u if present means unsigned, N is the 
     * number of bits in the integer
     *
     * find_type is used simply by giving first a type_list then the number of 
     * bits to search for.
     *
     * NB. Each type in the type list must had specified the template 
     * numeric_limits as it is used to compute the type len in (binary) digit.
     */
    typedef find_type<UNSIGNED_TYPES, 8>::type  uint8;
    typedef find_type<UNSIGNED_TYPES, 16>::type uint16;
    typedef find_type<UNSIGNED_TYPES, 32>::type uint32;
    typedef find_type<UNSIGNED_TYPES, 64>::type uint64;

    typedef find_type<SIGNED_TYPES, 7>::type    int8;
    typedef find_type<SIGNED_TYPES, 15>::type   int16;
    typedef find_type<SIGNED_TYPES, 31>::type   int32;
    typedef find_type<SIGNED_TYPES, 63>::type   int64;

}
于 2012-10-26T17:43:10.087 に答える
-1

あなたが述べたように、それはコンパイラとプラットフォームに大きく依存します。これについては、ANSI 標準http://home.att.net/~jackklein/c/inttypes.htmlを確認してください。

これは、Microsoft コンパイラ用のものです: Data Type Ranges

于 2009-02-26T08:06:08.467 に答える
-3

OpenGLQtなどのライブラリが提供する変数を使用できます。

たとえば、Qtqint8 (Qt がサポートするすべてのプラットフォームで 8 ビットであることが保証されています)、qint16、qint32、qint64、quint8、quint16、quint32、quint64 などを提供します。

于 2009-03-09T10:22:30.310 に答える
-10

64 ビット マシンの場合:

int: 4
long: 8
long long: 8
void*: 8
size_t: 8
于 2015-02-07T23:27:31.683 に答える
-12

サイズに基づく整数には、次の4つのタイプがあります。

  • 短整数:2バイト
  • 長整数:4バイト
  • long long integer:8バイト
  • 整数:コンパイラによって異なります(16ビット、32ビット、または64ビット)
于 2011-09-15T23:18:37.010 に答える