关于size_t(2)

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 是C/C++中基础的无符号整型数据。它是sizeof运算符的返回类型。选择这个类型是因为它能够保存各种数组的理论上的最大值。在一个32位系统中,size_t的大小是32bit,在64位系统中,它的大小是64bit。换一句话来说就是一个指针类型可以很安全的放在一个size_t的类型中(一个特殊的例外是类函数指针)。size_t通常用于循环,数组index,存储大小和地址运算。尽管size_t可以存储指针,但是,在使用的时候最好使用另外一个无符号整型uintptr_t来存储指针。在一些情况下使用size_t类型更有效。


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).


size_t是在C/C++的标准库中定义的一个基本的无符号整型。它的头文件在C语言中是stddef.h,在C++中的头文件是cstddef.h。在stddef.h中定义的类型在全局命名空间,而cstdef.h头文件把size_t放在命名空间std。鉴于C++已经因为兼容性的原因把C语言的标准头文件stddef.h包含了进去,所以在这些程序中你可以在全局命名空间 (::size_t, size_t) 和命名空间std (std::size_t)中都可以使用size_t。



In the terms of static analyzer PVS-Studio, type size_t refers to memsize-types. The analyzer includesViva64 system for detailed error detection in 64-bit programs and for code optimization. Many diagnostic messages shown by Viva64 analyzer relate to recommendations on using memsize-types. Using memsize-types (such as size_t, ptrdiff_t, INT_PTR) instead of 32-bit types in 64-bit programs allows you to:

  • enable the compiler to build a simpler and consequently faster code which will have no unnecessary conversions of 32-bit and 64-bit data. It is especially useful when operating with address arithmetic and array indexing;
  • avoid some errors when processing a large size of input data when the number of the elements being processed excesses the number UINT_MAX;
  • avoid some other more specific errors;
  • make the code more portable among 64-bit versions of Windows and Linux systems which use different data models. Thus, for example, for indexing large arrays in Linux systems you can use unsigned long type while in Windows it is impossible.

To learn more about the errors you can avoid when using size_t type and also how this type allows improving and optimizing your 64-bit programs, see the articles given in the references.

If you are planning to start developing 64-bit projects or porting the existing 32-bit projects on 64-bit systems, we would like to offer you purchasing PVS-Studio analyzer which will simplify this task greatly and allow you to avoid the long period of searching hidden errors.

http://www.viva64.com/en/t/0044/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值