一些基本的C/C++数据类型
2015-04-30 17:04
393 查看
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位相同,都是用long来定义的:
/usr/include/linux/types.h:54:typedef __kernel_time_t time_t;
/usr/include/asm/posix_types.h:21:typedef long __kernel_time_t;
size_type
the Standard really defines it as,
在32位下要注意,long(time_t\size_t\size_type)、char*、这一阵营占用都是4,表面上与int、uint32_t、unsigned一样
uint64_t始终是8个字节的(即使在32bit下,会定义为long long来保证的,long long在64bit下同long一样8字节)如下:
所以,在64bit下,[b]long(time_t\size_t\size_type\uint64_t)、char*这一阵营占用都是8,与int、uint32_t、unsigned真不一样了。[/b]
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位相同,都是用long来定义的:
/usr/include/linux/types.h:54:typedef __kernel_time_t time_t;
/usr/include/asm/posix_types.h:21:typedef long __kernel_time_t;
size_type
the Standard really defines it as,
template <class T> class allocator { public: typedef size_t size_type; //....... };
简单总结下:
long和指针char*在32位及64位下占用字节数一致,均是4和8.在32位下要注意,long(time_t\size_t\size_type)、char*、这一阵营占用都是4,表面上与int、uint32_t、unsigned一样
uint64_t始终是8个字节的(即使在32bit下,会定义为long long来保证的,long long在64bit下同long一样8字节)如下:
#if __WORDSIZE == 64 typedef unsigned long int uint64_t; #else __extension__ typedef unsigned long long int uint64_t; #endif
所以,在64bit下,[b]long(time_t\size_t\size_type\uint64_t)、char*这一阵营占用都是8,与int、uint32_t、unsigned真不一样了。[/b]
相关文章推荐
- C++的一些基本数据类型转换
- C++基础总结(1)--基本数据类型范围
- C++与Java基本数据类型比较
- 编译器定义的C/C++语言各种基本数据类型的取值范围
- Java——基本数据类型常用的一些方法
- C++基本数据类型
- C++基本数据类型及其表示范围
- c++ 基本数据类型字节数
- c++和java区别之基本数据类型
- C/C++的基本数据类型
- The New C++ -- 基本数据类型和字面值常量 (5. 宽字符类型和宽字符字面值常量)
- 编译器定义的C/C++语言各种基本数据类型的取值范围用程序输出
- C/C++ 基本数据类型
- C++基本数据类型与输入输出
- C++基本数据类型总结
- C++ 入门教程(六) 基本数据类型和变量
- C++基本数据类型
- C++数据类型:基本数据类型 构造数据类型(自定义数据类型)
- C/C++基本数据类型
- C++中预定义的运算符的操作对象只能是基本的数据类型