32-bit and 64-bit base

Source: Internet
Author: User

C language programming requires attention to the difference between 64-bit and 32-machine

The type of data, in particular int, is different in length under the platform of different bits of machine. The C99 standard does not specify the length size of the specific data type, only the level. To make the comparison:

16-bit Platform

Char 1 bytes 8 bits

Short 2 bytes 16 bit

int 2 bytes 16 bits

Long 4 bytes 32 bits

Pointer 2 bytes

32-bit Platform

Char 1 bytes 8 bits

Short 2 bytes 16 bit

int 4 bytes 32 bits

A long 4 bytes

A long long 8 bytes

Pointer 4 bytes

64-bit Platform

Char 1 bytes

Short 2 bytes

int 4 bytes

A long 8 bytes (difference)

A long long 8 bytes

Pointer 8 bytes (difference)

Second, programming considerations

In order to ensure the universality of the platform, the program should try not to use long database type. You can use a fixed-size macro definition of a data type that needs to refer to the Stdint.h header file:

typedef signed CHAR int8_t

typedef short INT int16_t;

typedef int int32_t;

# if __wordsize = = 64

typedef long int int64_t;

# Else

__extension__

typedef long long int int64_t;

#endif

Third, the use of int can also use intptr_t to ensure the universality of the platform, it is compiled on different platforms of different lengths, but are standard platform word length, such as 64-bit machine It is 8 bytes long, 32-bit machine its length is 4 bytes, using it can safely do integer and pointer conversion operations, This means that when you need to operate the pointer as an integer, it is safe to convert it to intptr_t. intptr_t needs to refer to the Stddef.h header file, which is defined as follows:

#if __wordsize = = 64

typedef long int intptr_t;

#else

typedef int intptr_t;

#endif

Use sizeof to calculate the size of the data type as much as possible in programming

The above type definitions have a corresponding unsigned type.

Iv. use of ssize_t and size_t

They are unsigned and signed size of computer word size respectively. They also represent the length of the computer, which is type int on a 32-bit machine and a long on a 64-bit machine. Using them is a great benefit for increasing the versatility of the platform, which in a sense equates to intptr_t and uintptr_t. Using them also requires referencing the stddef.h header file.

The socket's accept function is incorrect with size_t on some operating systems because the int* type received by accept, and the length of size_t may exceed the int* length limit, resulting in an error. Later BSD used sock_t to replace it.

32-bit and 64-bit base

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.