#include
#include
using namespace std;
enum MyEnum
{
a=0,
b=1,
EVO_MSG_REQUEST_IDENTIFIER_ALLOCATE = 3,
EVO_MSG_REPLY_IDENTIFIER_ALLOCATE = 4,
EVO_MSG_REQUEST_GRAPH_LIBRARY_QUERY = 5,
EVO_MSG_REPLY_GRAPH_LIBRARY_QUERY = 6
}MyEnum;
struct EvoVersion
{
char type;
char state;
char major;
char minor;
bool operator==(const EvoVersion & theVersion) const
{
return (type == theVersion.type
&& state == theVersion.state
&& major == theVersion.major
&& minor == theVersion.minor);
}
};
char m_CompName[128];
int main()
{
char *tmpBufMsg = new char[100];
strcpy_s(tmpBufMsg,7, "hello");
cout << sizeof(tmpBufMsg) << endl; //4
cout << strlen(tmpBufMsg) + 1 <<endl; //6
cout << sizeof(MyEnum) << endl;//4
cout << sizeof(a) << endl;//4
cout << sizeof(EvoVersion) << endl; //4
cout << sizeof(m_CompName) << endl; //128
unsigned int tt = 1;
cout << sizeof(tt) << endl; //4
unsigned long hh = 2;
cout << sizeof(hh) << endl; //4
system("pause");
return 1;
}
对sizeof的认知:
//对于非指针类型的数组,sizeof(数组名)长度是数组分配的大小,而不是数据类型所占的字节大小
char sz[] = "hello world!";
cout << "sizeof(sz)=" << sizeof(sz) << endl; //13 带有结束符
cout << "sizeof(char)=" << sizeof(char) << endl; //1个字节
cout << "sizeof sz=" << sizeof sz << endl; //13
cout << "sz =" << sz << endl; //hello world!
char str[8];
cout << "sizeof(str)=" <<sizeof(str)<< endl; //不是1 而是8
cout << "szieof(char)" << sizeof(char) << endl; //1
cout << "str=" << str << endl; //烫烫烫烫烫烫烫烫
int a = 7;
cout << "a=" << a<<endl; //7
cout << "sizeof(a)=" << sizeof(a) << endl; //4
cout << "sizeof(int)=" << sizeof(int) << endl; //4
//指针类型的数据 为四个字节 不论是(char *)、(int *)还是(void *)都是四个字节
char* msg = new char[1024];
cout << "sizeof(char*)" << sizeof(char*) << endl; //4
cout << "sizeof(msg)=" << sizeof(msg) << endl; //4
cout << "msg=" << msg << endl; //1024个屯
int* msgTest = new int [1024];
cout << "sizeof(int*)" << sizeof(int*) << endl; //4
cout << "sizeof(msgTest)=" << sizeof(msgTest) << endl; //4
cout << "msgTest=" << msgTest << endl; //0100BCA0 指向内存的地址
short i = 0;
cout << sizeof(short) << endl; //2
cout << sizeof(i) << endl; //2
bool flag = true;
cout << sizeof(bool) << endl; //1
cout << sizeof(flag) << endl; //1