Where is c++ size_t defined in linux

后端 未结 5 2068
你的背包
你的背包 2020-12-31 06:22

Now I\'m talking about new type definition by a programmer using typedef keyword. As long as my pupils are used to the type size_t (for example by using function length ()),

相关标签:
5条回答
  • 2020-12-31 06:41

    As others wrote, you probably can find it if you search all the include files. However the fact that this is how most implementations work is not guaranteed.

    The standard says that, for example, #include <stddef.h> should provide a definition for size_t which must be an unsigned integer type. But it also says that there's no need for a file called stddef.h to exist anywhere on the file system. And even an implementation that does provide such a file but that file only contains the following line

    #pragma stdlib_stddef_h
    

    would be perfectly conforming if the above pragma effectively provides what the standard prescribes for that header.

    In other words, size_t is an unsigned integer type because that's what the standard says, not because that's what you can read in a header file.

    0 讨论(0)
  • 2020-12-31 06:49

    std::size_t is defined in <cstddef>. See http://en.cppreference.com/w/cpp/header/cstddef.

    0 讨论(0)
  • 2020-12-31 06:50

    You can try to expand standard include files with the C preprocessor (cpp) by hand and check the output of that:

    $ echo '#include <stdlib.h>' | cpp -I/usr/include - > stdlib-expanded.c
    

    You will find that the output of cpp even includes markers to indicate from which files the code in stdlib-expanded.c has been included.

    0 讨论(0)
  • 2020-12-31 06:53

    gcc provides some of the headers and that is relevant here: size_t is defined in stddef.h which is one of those headers. Here it is for instance at /usr/lib/x86_64-redhat-linux/4.1.1/include/stddef.h. There the definition of size_t is

    typedef __SIZE_TYPE__ size_t;
    

    __SIZE_TYPE__ is a compiler predefined macro (which allows to ensure that the compiler and the header agree and, as what the compiler expect depend on its arguments -- for instance with -m32 it is an unsigned 32 bit bits, and with -m64 an unsigned 64 bits types --, to have the header independent of the compiler arguments).

    0 讨论(0)
  • 2020-12-31 06:54

    Just for completeness, have you considered simply asking C++ about size_t?

    #include <iostream>
    #include <cstddef>
    #include <limits>
    
    int main()
    {
        std::cout << "sizeof(size_t) = " << sizeof(std::size_t) << std::endl;
        std::cout << "is size_t an integer? " <<
            (std::numeric_limits<std::size_t>::is_integer ? "yes" : "no")
            << std::endl;
        std::cout << "is size_t signed? " <<
            (std::numeric_limits<std::size_t>::is_signed ? "yes" : "no")
            << std::endl;
    }
    

    gives me

    sizeof(size_t) = 8
    is size_t an integer? yes
    is size_t signed? no
    
    0 讨论(0)
提交回复
热议问题