c ++ in the various types of data type byte length occupied
First, a list of what data types in C ++ what are:
1, plastic: int, long
2, character: char, wchar_t
3, Boolean: bool
4, float: float, double
5, the pointer
First, plastic
int occupied by a memory cell size of the operating system. with the same long int
A previous 16-bit operating system is a 16-bit memory cell, it is 2 bytes; 32-bit system is a 32-bit memory cell, it is 4 bytes; 64-bit operating system is a 16-bit memory cell, so the accounting 8 bytes.
Second, character
char type generally occupies one byte, is used for the extended character set type wchar_t required to occupy two bytes.
Third, Boolean
occupies one byte bool
Fourth, the float
occupies 4 bytes float, double double float is 8 bytes
V. pointer
Pointer byte int length calculation principles are now almost digits to the operating system, and a pointer are equal, i.e., 32-bit system should be four bytes, 64-bit system should be 8 bytes.
code show as below
1 #include<iostream>
2
3 using namespace std;
4
5 int main()
6 {
7 int a;
8 long b;
9 char c;
10 wchar_t d;
11 bool e;
12 float f;
13 double g;
14 int*p= &a;
15
16 cout << sizeof(a) << endl;
17 cout << sizeof(b) << endl;
18 cout << sizeof(c) << endl;
19 cout << sizeof(d) << endl;
20 cout << sizeof(e) << endl;
21 cout << sizeof(f) << endl;
22 cout << sizeof(g) << endl;
23 cout << sizeof(p) << endl;
24 }
The results are as follows
qqtsj@qqtsj-Nitro-AN515-51:~/cpp$ g++ -o size1 size1.cpp qqtsj@qqtsj-Nitro-AN515-51:~/cpp$ ./size1 4 8 1 4 1 4 8 4
Its byte
1 #include<iostream> 2 3 using namespace std; 4 5 struct A 6 { 7 int a; 8 char b; 9 short c; 10 }; 11 12 struct B 13 { 14 char a; 15 int b; 16 short c; 17 }; 18 19 #pragma pack(2) 20 struct C 21 { 22 char a; 23 int b; 24 short c; 25 }; 26 27 #pragma pack(1) 28 struct D 29 { 30 int a; 31 char b; 32 short c; 33 }; 34 35 int main(int argc, char ** argv) 36 { 37 38 cout << sizeof(A) << " "<< sizeof (B) << " "<< sizeof (C) << " "<< sizeof (D) <<endl; 39 return 0; 40 } ~