glibc - #define void

Ori*_*ion 8 c glibc libc

我开始研究glibc(GNU Libc)以了解它是如何编写的.在malloc.c,我发现了一段代码如下:

#ifndef void
#define void        void
#endif
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释这意味着什么?不void总是定义?

谢谢

Mic*_*urr 8

我不确定其中的原因#define void void是什么malloc.c,但我的猜测如下:

正如Yasushi Shoji 提到的void并不总是 C 中的关键字。当它被引入/标准化时,能够使用 newvoid关键字与不支持它的编译器编译代码的常见解决方法是将其定义void为宏,例如:

#define void int   /* or maybe #define void char */
Run Code Online (Sandbox Code Playgroud)

该宏定义可以使用编译器命令行而不是通过标头来完成,这样就不需要确保所有翻译单元都包含定义宏的标头。

然而,迁移到 new 关键字的程序员使用如下所示的代码序列也可能很常见:

#ifndef void
#define void int
#endif
Run Code Online (Sandbox Code Playgroud)

例如,您将看到以下代码:

/*
 * This is a fairly bogus thing to do, but there seems to be no better way for
 * compilers that don't understand void pointers.
 */
#ifndef void
#define void char
#endif
Run Code Online (Sandbox Code Playgroud)

http://buildbot.zope.org/Squid-2.4STABLE6%2B/include/snmp_impl.h?annotate=1.1.1.1&cvsroot=Zope.org

所以,我的猜测是, in#define void void只是malloc.c一种防止其包含的标头中可能存在的任何此类序列重新定义的方法void,但仍然允许它先前定义为“gloablly”(在malloc.c之前只有注释#define void void)如果它是在不支持的配置上编译的void。换句话说,如果在开始编译void之前没有全局定义为宏malloc.c,则没有理由在编译过程中稍后将某些内容定义为其他内容。


Yas*_*oji 7

看看git的历史,它是这样的:

/*
  Void_t* is the pointer type that malloc should say it returns
*/

#ifndef Void_t
#if (__STD_C || defined(WIN32))
#define Void_t      void
#else
#define Void_t      char
#endif
#endif /*Void_t*/
Run Code Online (Sandbox Code Playgroud)

这是历史[C]的一种解决方法,它没有void并且malloc()返回char *而不是void.该代码在2011年由Ulrich Drepper删除.提交似乎不是由脚本或任何自动生成的,因此他必须有他的意图来定义.

提交消息没有说明void:

简化malloc代码

删除所有未使用的配置选项和死代码.