1. 例题
#include <iostream>
class A {};
class B { char m_data; };
class C {
char m_data[100];
};
class D {
char *m_data;
D(): m_data(new char[100]) {}
~D() { delete []m_data; }
};
static void testSizeof() {
std::cout << "bool sizeof " << sizeof(bool) << std::endl;
std::cout << "char sizeof " << sizeof(char) << std::endl;
std::cout << "int sizeof " << sizeof(int) << std::endl;
char data[10];
char *pData = data;
std::cout << "char data[10] sizeof " << sizeof(data) << std::endl;
std::cout << "char* sizeof " << sizeof(pData) << std::endl;
std::cout << "A sizeof " << sizeof(A) << std::endl;
std::cout << "B sizeof " << sizeof(B) << std::endl;
std::cout << "C sizeof " << sizeof(C) << std::endl;
std::cout << "D sizeof " << sizeof(D) << std::endl;
std::cout << "size_t sizeof " << sizeof(std::size_t) << std::endl;
};
static void unsignedAndSigned() {
std::size_t a = 10;
int b = -1;
if (b < a)
std::cout << "right
";
else
std::cout << "bad
";
}
int main()
{
testSizeof();
unsignedAndSigned();
return 0;
}