__PTRDIFF_TYPE__与ptrdiff_t的 [英] __PTRDIFF_TYPE__ vs. ptrdiff_t

查看:1139
本文介绍了__PTRDIFF_TYPE__与ptrdiff_t的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么GNU C编译器使用2个不同的宏几乎相似的名字 - __ PTRDIFF_TYPE __ ptrdiff_t的 - ,随着时间的,因为它们是作为语义相同

I do not understand why the gnu C compiler uses 2 different macros with almost similar names -- __PTRDIFF_TYPE__ and ptrdiff_t -- , as time as they are identical as semantics.

这些宏的定义是这里

我问过类似的问题,关于这个问题

我复制的问题,因为在我的第一篇我没有收到答案,我真正需要的。

I duplicate the question because in my first post I did not receive an answer I really need.

有人可以详细解释清楚的GNU C编译器如何使用他们每个人的?

Can somebody clearly explain in detail how the gnu C compiler uses each of them ?

编辑:

此交的目的是从时刻了解的gcc具体使用此对象(指针差分型)时产生并且当语义检查结束并产生集成电路插入解析器的时刻词位流。

The purpose of this post is to understand how gcc concretely uses this object (pointer difference type) from the moment when the lexeme stream is generated and inserted in the parser to the moment when the semantic check is finished and IC is generated.

推荐答案

ptrdiff_t的是不是一个宏,它是一个typedef名称,由定义STDDEF.H 。它是在C标准的两个指针之间的差的类型指定。

ptrdiff_t is not a macro, it is a typedef name, defined by stddef.h. It is specified in the C standard as the type of the difference between two pointers.

__ __ PTRDIFF_TYPE 是GCC的 STDDEF.H 的实现细节。除非你正在编写一个C运行时库你不应该使用它。

__PTRDIFF_TYPE__ is an implementation detail of GCC's stddef.h. You should not use it unless you are writing a C runtime library.

同时存在的原因是,C编译器是不允许定义 ptrdiff_t的无条件的。这名只有当你获得包括其标准规定的含义 STDDEF.H ;这也可以在应用程序中使用,否则。 (注意,不像C ++,在C标准库头是的的允许[表现为,如果他们]包括对方。) __ PTRDIFF_TYPE __ ,另一方面,是出了名的编译器的的允许无条件确定的,因为它与两个下划线开始。因此, __ PTRDIFF_TYPE __ 是无条件pdefined $ P $和 STDDEF.H 使用它来定义 ptrdiff_t的在适当的时候。

The reason both exist is that the C compiler is not allowed to define ptrdiff_t unconditionally. That name only acquires its standard-specified meaning if you include stddef.h; it's available for application use otherwise. (Note that unlike C++, in C the standard library headers are not allowed to [behave as-if they] include each other.) __PTRDIFF_TYPE__, on the other hand, is a name that the compiler is allowed to define unconditionally, because it begins with two underscores. So __PTRDIFF_TYPE__ is predefined unconditionally, and stddef.h uses it to define ptrdiff_t when appropriate.

和原因 STDDEF.H 经过这间接,而不是裸

And the reason stddef.h goes through this indirection, rather than having a bare

typedef long int ptrdiff_t;  /* or whatever */

是因为定义可能需要根据编译模式而变化。例如,在X86-64 / Linux上, ptrdiff_t的长整型在默认模式,但 INT -m32 模式。编译器必须知道用于两个指针的不同而整数类型,因此它可能也暴露出该信息,而不是让 STDDEF.H 全部重复相同的逻辑。

is because the definition might need to vary depending on compilation mode. For instance, on x86-64/Linux, ptrdiff_t is long int in the default mode but int in -m32 mode. The compiler has to know which integer type to use for the difference of two pointers, so it may as well expose that information rather than making stddef.h repeat all the same logic.

(你引GCC的文档是对这个很清楚,只要你阅读整个段落:

(The GCC documentation you cited is quite clear on this, provided you read the entire paragraph:

这些宏定义正确的基础类型[typedef名称一堆标准规定。 它们的存在使标准头文件 STDDEF.H stdint.h wchar.h 正常工作。你不应该直接使用这些宏;相反,包括相应的​​头文件,并使用类型定义。

These macros are defined to the correct underlying types for [a bunch of standard-specified typedef names]. They exist to make the standard header files stddef.h, stdint.h, and wchar.h work correctly. You should not use these macros directly; instead, include the appropriate headers and use the typedefs.

重点煤矿。)

这篇关于__PTRDIFF_TYPE__与ptrdiff_t的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆