Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between a 32-bit program and a 64-bit program

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the difference between 32-bit programs and 64-bit programs?" the content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the differences between 32-bit programs and 64-bit programs.

Differences in code

In fact, there is not much difference in code between 32-bit programs and 64-bit programs, strictly speaking, even the same, the main difference is that some basic data types occupy different byte lengths (Note: this is only for Unix-like platforms)

Type 32-bit occupancy byte 64-bit occupancy byte long48unsigned long48 pointer 48

Of course, the long here includes some types defined by it, such as time_t, and its length is also different. there is also an interesting question about time_t, "what is the 2038 problem?"

In addition, the default number of aligned bytes is also different, 4 bytes for 32-bit programs and 8 bytes for 64-bit programs. For byte alignment, please refer to "managing byte alignment".

Differences in executable files

Let's take a look at a small example to see the difference between them.

/ / Source: official account programming Zhuji / / author: Mr. Wangwang test.c # include struct Test {int a; long b;}; int main (void) {printf ("sizeof (long) =% zu\ n", sizeof (long)); / / long type occupied bytes printf ("sizeof (unsigned long) =% zu\ n", sizeof (unsigned long)); / / unsigned long type occupied bytes struct Test test = {1pc2} Printf ("sizeof (struct Test) =% zu\ n", sizeof (test)); / / used to test alignment bytes printf ("sizeof (pointer) =% zu\ n", sizeof (& test)); / / pointer occupies return 0;}

If your system is 64-bit, it compiles to 64-bit programs by default, and if you need to compile to 32-bit programs, you need to take the-M32 parameter. If your system is 32-bit, you can't run 64-bit programs directly. But if it's 64-bit, you can run 32-bit programs. (in fact, when you need to choose the number of digits when downloading software, you should be aware that if your system is 32-bit, but you download a 64-bit package, it is naturally not available, but vice versa.)

Compile to run as a 32-bit program:

$gcc-o test32 test.c-M32 $. / test32 sizeof (long) = 4 sizeof (unsigned long) = 4 sizeof (struct Test) = 8 sizeof (pointer) = 4

The compiled bit 64-bit program runs:

$gcc-o test64 test.c $. / test64 sizeof (long) = 8 sizeof (unsigned long) = 8 sizeof (struct Test) = 16 sizeof (pointer) = 8

From the running results, we can also see the differences mentioned above.

So what's the difference between the executable itself?

$readelf-h test32 ELF Header: Magic: 7f 45 4c 46 01 01 00 00 00 Class: ELF32 Data: 2's complement Little endian Version: 1 (current) OS/ABI: UNIX-System V ABI Version: 0 Type: EXEC (Executable file) Machine: Intel 80386 (...)

You can see that the Class property is identified as ELF32.

For 64-bit:

Readelf-h test64 ELF Header: Magic: 7f 45 4c 46 02 01 00 00 00 Class: ELF64 Data: 2's complement Little endian Version: 1 (current) OS/ABI: UNIX-System V ABI Version: 0 Type: EXEC (Executable file) Machine: Advanced Micro Devices X86-64

Its property is ELF64. In fact, we can find a lot of information through readelf.

For example, if you have a link error after cross-compilation or the last executing program does not work on the target machine, check the Machine section to see if the program can run on the platform you want.

For example, the Machine in a 64-bit program shows Advanced Micro Devices X86-64, which at least means it doesn't work properly on the arm platform.

How much memory can a program apply for?

Do you remember this interview question? If you just answer the Linux theory that the maximum is no more than 3G and no more than G, it is certainly incomplete, and there must be a distinction between 32-bit programs and 64-bit programs.

This has been mentioned in "Why did dereference NULL hang up?" 32-bit determines that the maximum value of its virtual address space is 2 ^ 32, that is, 4G, except for about 1G occupied by the operating system, leaving about 3G. Of course, 3G contains all the code, data, etc., and the summary is that, in the end, it can be used no more than 3G. Less than 3G address space. (note this does not mean that it can only access the computer's 4G memory, but that the maximum addressing range is 4G). So the 64-bit virtual address space is extended to 17179869184G, so do you see the difference?

Through the above simple analysis, we can find that the memory that can be used by 64-bit programs is amazing, while 32-bit programs are very limited. in addition, there is another question mentioned in "what is the 2038 problem". That is, after 2038, 32-bit programs will be difficult to use time-related processing normally.

Of course, 64-bit systems can usually support higher-precision floating-point operations.

Support both 32-bit and 64-bit coding principles

For the reasons mentioned earlier, many traditional systems have begun to migrate to 64-bit systems, and if the original code is very standard, the migration is relatively easy, linking 64-bit libraries and compiling them into 64-bit programs. however, if you do not follow the following principles, then the workload is relatively large:

Depends on the amount of space occupied by long types and pointer types and their representation range

Of course, there may be a lot of expression for this principle.

Mixed use of long and int

For example:

Void test (long len) {int localLen = len; xxxx;}

It is obvious that a truncation may be found here. The most common ones are:

Int len = sizeof (xxx)

Of course, it won't be too much of a problem here in most cases, until its length is larger than the range indicated by int.

Be careful with mask definition

We may often need to define some masks:

Long mask = OxFFFFFFFFL

On 32-bit systems, this sets all bits (all 1 for each), but on 64-bit systems, only the lower 32 bits are set. The result is that the value is 0x00000000FFFFFFFF.

If you want all locations 1, you can:

Long mask = 1L

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report