C++ linux中定义的c++ size_t在哪里
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12490622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is c++ size_t defined in linux
提问by Stefan Majewsky
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 funciton length ()), for which I had to ask them a little effort to just "believe" it is an integer type, I think it woud be great to show noew them where this type is defined.
现在我正在谈论程序员使用 typedef 关键字的新类型定义。只要我的学生习惯了 size_t 类型(例如通过使用函数长度()),为此我不得不要求他们稍微努力“相信”它是一个整数类型,我认为这会很棒向他们显示此类型的定义位置。
So, I've done a lot grep's in /usr/include in an ubuntu box, and what I see is that size_t is, in turn, a redefinition of size_type wich in turn is a redefinition of metadata_type, and that's the end in this directory. Not found the final "typedef unsigned int metadata_type;".
因此,我在 ubuntu 框中的 /usr/include 中做了很多 grep,而我看到的是 size_t 是 size_type 的重新定义,反过来又是 metadata_type 的重新定义,这就是结束目录。没有找到最终的“typedef unsigned int metadata_type;”。
In /usr/src I've found anohter previous type called yy_size_t,...
在 /usr/src 中,我发现了另一个以前的类型,称为 yy_size_t,...
But, in any case I've been unable the get to the end of the chain.
但是,无论如何,我一直无法到达链条的末端。
Does anyone know where to find out the final definition to check out that it is an unsigned int (or the like)? May be I miss a development package in my box? In this case, why I'm able to compile programs using size_t type?
有谁知道在哪里可以找到最终定义来检查它是否是 unsigned int(或类似的)?我可能会错过盒子里的开发包吗?在这种情况下,为什么我能够使用 size_t 类型编译程序?
回答by Stefan Majewsky
You can try to expand standard include files with the C preprocessor (cpp
) by hand and check the output of that:
您可以尝试使用 C 预处理器 ( cpp
) 手动扩展标准包含文件并检查其输出:
$ 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.
您会发现cpp
even的输出包含标记,以指示stdlib-expanded.c
已包含哪些文件中的代码。
回答by AProgrammer
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
gcc 提供了一些头文件,这在这里是相关的:size_t
定义stddef.h
其中是这些头文件之一。例如,这里是/usr/lib/x86_64-redhat-linux/4.1.1/include/stddef.h
。那里的定义size_t
是
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).
__SIZE_TYPE__
是编译器预定义的宏(它允许确保编译器和头文件一致,并且编译器期望依赖于它的参数——例如,-m32 是一个无符号的 32 位位,而 -m64 是一个无符号的 64 位)位类型 --,使标头独立于编译器参数)。
回答by Analog File
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.hto exist anywhere on the file system. And even an implementation that does provide such a file but that file only contains the following line
标准说,例如,#include <stddef.h>
应该提供一个size_t
必须是无符号整数类型的定义。但它也表示不需要在文件系统的任何地方存在一个名为stddef.h的文件。甚至提供这样一个文件但该文件只包含以下行的实现
#pragma stdlib_stddef_h
would be perfectly conforming if the above pragma effectively provides what the standard prescribes for that header.
如果上述 pragma 有效地提供了标准为该标头规定的内容,则将完全符合。
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.
换句话说, size_t 是无符号整数类型,因为这是标准所说的,而不是因为这是您可以在头文件中读取的内容。
回答by Useless
Just for completeness, have you considered simply asking C++ about size_t
?
只是为了完整性,您是否考虑过简单地询问 C++ 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
回答by R Sahu
std::size_t
is defined in <cstddef>
. See http://en.cppreference.com/w/cpp/header/cstddef.
std::size_t
中定义<cstddef>
。请参阅http://en.cppreference.com/w/cpp/header/cstddef。