In C++, char, short, long, double, and pointer occupy several bytes, and in 32-bit machine


char occupies 1 byte, short occupies 2 bytes, int, float, long all occupy 4 bytes, and double occupies 8 bytes

The pointer length is related to the address bus. Because the pointer records an address, then 32-bit is 4 bytes, 64-bit is 8 bytes.

I found a problem. The following code is run under win10 64-bit system. It should be said that int occupies 8 bytes. Later, I thought wrong; the project I created is a win32 console application, which means that the compiler is 32-bit. -- "How many bytes an int occupies is determined by the compiler as well as by the CPU or virtual machine or operating system, but in the final analysis it is determined by the compiler."

64-bit win10, codeblocks win32 console

#include "iostream"
using namespace std;

int main()
{
double *p = NULL;
cout<<sizeof(char)<<endl;
cout<<sizeof(short)<<endl;
cout<<sizeof(int)<<endl;
cout<<sizeof(float)<<endl;
cout<<sizeof(long)<<endl;
cout<<sizeof(double)<<endl;
cout<< sizeof(char *) << sizeof(short *) << sizeof(int *) << sizeof(p) << sizeof(double *)<<endl;
return 0;
}

The result is:

1
2
4
4
4
8
44444


As can be seen from the above, char occupies 1 byte, short occupies 2 bytes, int, float, long all occupy 4 bytes, double occupies 8 bytes, and any type of pointer occupies 4 bytes;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325648742&siteId=291194637