Number of bytes per data type under 32-bit, 64-bit operating system

Source: Internet
Author: User

Click to open link

In 32-bit, 64-bit systems, the only change is the length of the pointer; The 32-bit system is 4 bytes, 64 bits is 8 bytes. This refers to the bit width of the register, which is the so-called three-position .

Results under 32-bit platform:


Results under 64-bit platform:


One, the following points are worthy of our attention:

1, about the value range of int, the default int value range is determined by the compiler designer, which is usually the most natural and efficient number of bits of the machine. Even if we are on a 32-bit machine, we can set short, int, and long to 32 bits if there is no instruction to handle the shorter integer value efficiently.

2, floating point number in the default case is a double, but you can add l/l or f/f to indicate its long double or float data type.

3, when the porting problem is more important, the best solution is to limit the char value used between the unsigned char and the signed Char range. And only when the declared signed Char and unsigned char are displayed, the arithmetic operation is performed on it, considering that Char is designed to store characters, but in many practical applications, especially in embedded product design, we use it to store small integers in essence, Different compilers treat the default char as unsigned or signed, but some machines are handy for handling signed char, hard to turn it into unsigned, and the efficiency can be compromised. So there are some inconsistencies in the point that all of the statements shown are unsigned char and signed char are not the only strategy.

Second, under 32-bit operating systems: 1: Shaping

int 4 bytes

Longint 4 bytes

Shortint 2 bytes

Unsignedint 4 bytes

Unsignedlong int 4 bytes

Unsignedshort int 2 bytes

2: Character type

Char 1 bytes

Unsignedchar 1 bytes

3: Floating-point type

Float 4 bytes

Double 8 bytes

Longdouble 8 bytes

Unsigned longdouble 8 bytes

Unsigneddouble 4 bytes

4: String type

String 32 bytes

5: pointer type

All types of pointers are 4 bytes

6: function

except void type. All other functions occupy a number of bytes equal to the number of bytes that the function's return type occupies. is not related to the inside of the function body.

such as: float fun () {return 2;}

sizeof (fun ()) = 8

7: Structure, class

Internal data types occupy the sum, note the boundary alignment.

such as: struct FUN1
{
int A; 4
Double b; 8
char c; 1
};

sizeof (FUN1) =24

Truct fun2
{
int A; 4
char c; 1
};

sizeof (FUN2) =8

Special:

Structfun3
{
String A; 32
Char b,c,d; 3

}b;

sizeof (FUN3) =36

8: Consortium Union

The number of bytes that occupy the data type that occupies the largest number of bytes.

Third, for 64-bit operating system should pay attention to the following points:

64-bit Advantage: 64-bit applications can directly access 4EB of memory and file size up to 4 EB (2 of 63 power); Access to large databases. This article describes the 64-bit under C language Development program considerations.

1.32-bit and 64-bit C data types

32 and 64-bit C language built-in data types, as shown in the following table:


i Span style= "color:red" >int type
L long type
P means: pointer Span style= "color:red" > pointer type
32 bit system
up 64 bit system
    For example: LP64 indicates that long and pointer types under 64-bit systems are 64 bits long.
    64-bit Linux uses the LP64 standard, which is that long and pointer type lengths are 64 bits, the lengths of the other types are the same as the lengths of the same types under 32-bit systems, and the lengths of 32-bit and 64-bit types are compared to the blue ones.
    The length of the data type detected by sizeof with 32 and 64-bit Linux systems.
32-bit platform results:


Results under 64-bit platform:


2.64 System Development Considerations:

2.1 Format string: Long uses%ld and the pointer uses%p, for example:

1. char *ptr = &something;

2. printf (%x\n ", PTR);
The above code is - not correct on the bit system, only show Low 4 the contents of the byte. The correct method is: Use %p, as follows:

1. char *ptr = &something;

2. printf (%p\n ", PTR);

2.2 Numeric constants: Constants to add L
Example 1, constant 0xFFFFFFFF is a signed long type. On a 32-bit system, this will place all bits (1 per bit), but on a 64-bit system, only the low 32 bits are set, and the result is that the value is 0x00000000ffffffff.
Example 2, in the following code, the maximum value of a can be 31. This is because 1 << A is of type int.

1. long l = 1 << A;

to be in - displacement on a bit system, you should use the 1L , as shown below:

1. long l = 1L << A;

2.3 Symbol Extension: Avoid signed and unsigned number operations, for example:

1. int i =-2;

2. unsigned int j = 1;

3. long L = i + j;

4. printf ("Answer:%ld\n", L);

32 bits under IS-1, under 64 bits is 4294967295. The reason is that the expression (I+J) is an unsignedint expression, but when it is assigned to K, the sign bit is not expanded. To solve this problem, the operands on both ends are either signed or unsigned.
2.4 Conversion truncation (the truncation effect occurs whenever a data type with a large number of bits is converted into a small number of bits, which results in a data error, and the system typically automatically converts data types when the data types are being calculated, but at this point the data type with a small number of bits is automatically converted into a data type with a large number of digits. This does not result in data errors):
Conversion truncation occurs when you convert a long to int, as in the following example:

1. int length = (int) strlen (str);

Strlen returns size_t (which is unsignedlong in LP64), and when assigned to an int, truncation is inevitable. In general, truncation only occurs when the length of STR is greater than 2GB, which generally does not occur in a program. However, the appropriate polymorphic types (such as size_t, uintptr_t, and so on) should be used as much as possible.

2.5 Assignment:
Do not swap using int and long types, for example:

1. int i;

2. Long int a = l;

3. i = A;

Do not use the int type to store pointers, for example:

1. unsigned int i, *ptr;

2. i = (unsigned) ptr;

Do not use pointers to hold values of type int. For example:

1. int *ptr;

2. int i;

3. ptr = (int *) i;


2.6 Performance in a 64-bit environment:

After porting to a 64-bit platform, performance is actually reduced. The reason is that the length of the pointer in the 64-bit is related to the size of the data, and this raises the cache hit ratio, data alignment, and so on. By changing the order in which the data is arranged in the structure, the storage space is reduced because the data is less populated. Such as:


2.7 Libraries that are linked to in the program will use a 64-bit library.

all the problems are visible by the Long and pointer length changes, the length of long and pointer types is only remembered during development.


Number of bytes per data type under 32-bit, 64-bit operating system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.