-6

C++ でデータ型が保持するバイト数を調べたいのですが、どこから始めればよいかわかりません。私はグーグルで検索しましたが、何も見つかりませんでした。

4

2 に答える 2

2

I dont know how you happen to miss out on sizeof.

In the programming languages C and C++, the unary operator sizeof is used to calculate the size of any datatype. The sizeof operator yields the size of its operand with respect to the size of type char.

sizeof ( type-name )

Refer to know more here : MSDN

Following is the example from MSDN :

size_t getPtrSize( char *ptr )
{
   return sizeof( ptr );
}

using namespace std;
int main()
{
   char szHello[] = "Hello, world!";

   cout  << "The size of a char is: "
         << sizeof( char )
         << "\nThe length of " << szHello << " is: "
         << sizeof szHello
         << "\nThe size of the pointer is "
         << getPtrSize( szHello ) << endl;
}
于 2013-07-24T10:13:58.443 に答える
1

sizeof 演算子を使用する

#include <iostream>
using namespace std;
int main()
{
    cout << "bool:\t\t" << sizeof(bool) << " bytes" << endl;
    cout << "char:\t\t" << sizeof(char) << " bytes" << endl;
    cout << "wchar_t:\t" << sizeof(wchar_t) << " bytes" << endl;
    cout << "short:\t\t" << sizeof(short) << " bytes" << endl;
    cout << "int:\t\t" << sizeof(int) << " bytes" << endl;
    cout << "long:\t\t" << sizeof(long) << " bytes" << endl;
    cout << "float:\t\t" << sizeof(float) << " bytes" << endl;
    cout << "double:\t\t" << sizeof(double) << " bytes" << endl;
    cout << "long double:\t" << sizeof(long double) << " bytes" << endl;
    return 0;
}

出力:

bool:       1 bytes
char:       1 bytes
wchar_t:    2 bytes
short:      2 bytes
int:        4 bytes
long:       4 bytes
float:      4 bytes
double:     8 bytes
long double:    12 bytes

使用 MinGW g++ 4.7.2 Windows

于 2013-07-24T10:17:19.943 に答える