• 一些基本的C/C++数据类型


    • 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,

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

     简单总结下:

    long和指针char*在32位及64位下占用字节数一致,均是4和8.

    在32位下要注意,long(time_tsize_tsize_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下,long(time_tsize_tsize_typeuint64_t)、char*这一阵营占用都是8,与int、uint32_t、unsigned真不一样了。

  • 相关阅读:
    网页加载进度条
    BFC
    Java — 基础语法
    Python — 多进程
    Python — 进程和线程
    Python — 面向对象进阶2
    Python — 面向对象编程基础
    Linnux 05
    Linnux 04
    Linnux 03
  • 原文地址:https://www.cnblogs.com/leby/p/4469358.html
Copyright © 2020-2023  润新知