複習一些基本的C/C++資料類型

來源:互聯網
上載者:User

標籤:

  • size_t

size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof operator. The type‘s size is chosen so that it could store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits and on a 64-bit one - 64 bits. In other words, a pointer can be safely put inside size_t type (an exception is class-function-pointers but this is a special case). size_t type is usually used for loop, array indexing, size storage and address arithmetic. Although size_t can store a pointer, it is better to use another unsinged integer type uintptr_t for that purpose (its name reflects its capability). In some cases using size_t type is more effective and safe than using a more habitual for the programmer unsigned type.

size_t is a base unsigned integer memsize-type defined in the standard library of C/C++ languages. This type is described in the header file stddef.h for C and in the file cstddef for C++. Types defined by the header file stddef.h are located in the global namespace while cstddef places the size_t type in the namespace std. Since the standard header file stddef.h of the C language is included into C++ programs for the purpose of compatibility, in these programs you may address the type both in the global namespace (::size_t, size_t) and namespace std (std::size_t).

32:
/usr/include/asm/posix_types.h:18:typedef unsigned int     __kernel_size_t;
/usr/include/linux/types.h:39:typedef __kernel_size_t              size_t;
64:
/usr/include/asm-i386/posix_types.h:18:typedef unsigned int        __kernel_size_t;
/usr/include/asm-x86_64/posix_types.h:18:typedef unsigned long     __kernel_size_t;
/usr/include/linux/types.h:39:typedef __kernel_size_t              size_t;

  • ptrdiff_t

A basic signed integer C/C++ type. The type‘s size is chosen so that it could store the maximum size of a theoretically possible array of any type. On a 32-bit system ptrdiff_t will take 32 bits and on a 64-bit one - 64 bits. In other words, a pointer can be safely put inside ptrdiff_t type (an exception is class-function-pointers but this is a special case). By the way the result of the expression where one pointer is subtracted from another (ptr1-ptr2) will have ptrdiff_t type. ptrdiff_t type is usually used for loop, array indexing, size storage and address arithmetic. ptrdiff_t type has its synonym intptr_t whose name indicates more clearly that it can store a pointer. In some cases using ptrdiff_t type is more effective and safe than using a more habitual for the programmer int type.

  • time_t

time_t 在32和64位相同:
/usr/include/linux/types.h:54:typedef __kernel_time_t              time_t;
/usr/include/asm/posix_types.h:21:typedef long             __kernel_time_t;

在32bit下,time_t的儲存要注意,不等同於size_t\unsigned等。

size_t\prtdiff_t\time_t都是基於long的,所以與伺服器相關!

  • size_type

the Standard really defines it as,

template <class T> class allocator {   public:       typedef size_t size_type;       //.......};

 

複習一些基本的C/C++資料類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.