关于size_t

来源:互联网 发布:超人电力工程造价软件 编辑:程序博客网 时间:2024/05/13 02:01

size_t 类型

size _t 为了增强程序的可移植性,便有了size_t ,不同系统上,定义size_t可能不一样。

经测试发现,在32位系统中size_t是4字节的,在64位系统中,size_t是8字节的,这样利用该类型可以增加程序移植性。

size_t的定义 

它的定义在/usr/include/linux/types.h

                    typedef _kernel_size_t size_t;

 

跟体系结构相关                           

而__kernel_size_t 定义在 /usr/include/asm/posix_types.h                          

安装的是内核的源码

asm-i386/posix_types.h

              typedef unsigned int __kernel_size_t;

asm-1a64/posix_types.h

              typedef unsigned long kernel_size_t;

 

size_t一般用来表示一种计数,比如有多少东西被拷贝等。例如:sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小。 它的意义大致是“适于计量内存中可容纳的数据项目个数的无符号整数类型”。所以,它在数组下标和内存管理函数之类的地方广泛使用。


http://www.cnblogs.com/li-hao/archive/2013/02/27/2935100.html



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

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
原创粉丝点击