Recognize the size of size_t and pointer types

Source: Internet
Author: User

Space occupied by 1.size_t

For size_t exactly what type, Baidu Encyclopedia has carried on the first close explanation.

size_t Overview : The size_t type is defined in the Cstddef header file, which is the C + + version of the header file stddef.h of the standard library. It is a machine-related unsigned integral type that is large enough to guarantee the size of the objects in memory.

size_t Origin : in C + +, the design of size_t is to adapt to multiple platforms. The introduction of size_t enhances the portability of programs on different platforms.

size_t size : About size_t occupied space Baidu Encyclopedia description is: tested found that in 32-bit systems size_t is 4 bytes, and in 64-bit systems, size_t is 8 bytes, so the use of this type can enhance the portability of the program.

The question Baidu Encyclopedia and the online ape friend's description seems to be very reasonable, but many people in the test time discovered, why the test environment is clearly the 64-bit system, sizeof (size_t) The value is equal to 4? Rather than the original expected 8.

The native environment is Win7 64bits, using VS2012 to verify.

Native System type:

Test code:

cout<<"sizeof(size_t)="<<sizeof(size_t)<<endl;

Output Result:

question Answer : why this, I have been not understand. Originally said on the net size_t size is determined by the system's number of digits is inaccurate. What is the size of the size_t decision?

First look at the compilation configuration of the VS2012 I just tested the code. The configuration is as follows:


What does the Win32 in the red box mean? The original Win32 indicates that the generated program is 32bits. 32bits programs can run under Windows 32bits Systems or under Windows 64bits systems. So, we configured the generated program to be 32bits, so size_t is the unsigned int type, which is 4 bytes in size.

The definition of size_t type in VC + + is as follows:

#ifdef  _WIN64typedefunsigned __int64    size_t;#elsetypedefunsignedint   size_t;#endif

Its approximate meaning is size_t either unsigned int, or unsigned long int, then according to the above reasoning, modify the compilation option to x64, generate 64bits program, size_t type is not become unsigned long int out? Verify the following:

The compilation configuration changes for VS2012 are as follows:

The same test code:

cout<<"sizeof(size_t)="<<sizeof(size_t)<<endl;

The output is:

As expected, size_t becomes a unsigned long int, taking up 8 bytes of memory space.

Summary : The size of the size_t is not determined by the number of bits in the system, as many online descriptions describe. The size of the size_t is determined by the type of program you generate, but the type of program that is generated is related to the type of system. The 32bits program can be run on a 64bits system or on a 32bits system. However, the 64bits program can only run on a 64bits system. However, the program we compile is generally 32bits, so the size of the size_t becomes 4 bytes.

2. The size of the pointer

About the size of the pointer, the online description is basically a uniform, that the pointer is a storage address, if the 32-bit machine is 4 bytes, if the 64-bit machine is 8 bytes, according to the machine word and decided.

What are the 32-bit machines and 64-bit machines here? I think the CPU architecture determines the type of machine, if the CPU is the x86 architecture, then the 32-bit CPU, of course, not all the x86 architecture CPU is 32 bits, such as Intel's 8086 and 8088 is 16-bit CPU.

If the CPU is a x86-64 architecture, then it is a 64-bit CPU. The number of bits of the CPU is determined by its word length, and the word length indicates that the CPU is able to handle the number of bits in a binary number at the same time, called word length. The word length is determined by the number of bits in the CPU, not by the width of the data bus, but the width of the data bus is generally the same as the number of bits in the CPU.

The number of bits of the system is dependent on the number of CPUs, that is, 32-bit CPUs can not be installed 64-bit systems, but now (2015) CPU is basically x86-64 CPU, all support 64-bit system. But as discussed above, if the compiled generated program is not 64-bit, then the size of the pointer is still 4 bytes.

Verify the following:

The compiler configuration for VS2012 is as follows:

Test code:

cout<<"sizeof(char*)="<<sizeof(char*)<<endl;

Output Result:

To change the compilation configuration and generate the 64-bit program, we will get the expected results:

Resources

[1] Baidu encyclopedia. size_t

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Recognize the size of size_t and pointer types

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.