g++中宏NULL究竟是什么?

img_e25d4fb2f8de1caf41a735ec53088516.pngg++中宏NULL究竟是什么?.pdf

NULL是个指针,还是个整数?0?或(void*)0?答案是和g++版本有关。g++ 4.6支持C++11,引入了nullptr,也许会发生变化。

 

可以写段简单代码求证一下:

#include 

#include 

 

int main()

{

        printf("NULL: %d\n", NULL);

        printf("sizeof(NULL): %d\n", sizeof(NULL));

        printf("typeid(__null).name(): %s\n", typeid(__null).name());

        printf("typeid(0).name(): %s\n", typeid(0).name());

        return 0;

}

 

使用g++ 4.1.2SuSE 10.1)编译,输出结果如下:

NULL: 0

sizeof(NULL): 8

typeid(__null).name(): l

typeid(0).name(): i

 

从输出结果,可以看到NULLlong类型的整数,定义应当是0L0LL

 

下面再借助gdb,来看一看它的真面目(博文:《GDB高级技巧》有介绍gdb的高级使用)。假设源文件名为x.cpp,使用如下方法编译:

g++ -g3 -gdwarf-2 -o x x.cpp

 

注意这里使用了参数“-g3 -gdwarf-2”。其中“-g3”不能是“-g”。

编译成功后,在gdb中跟踪运行程序,在main函数处打断点即可:

adoop@VM-39-166-sles10-64:~/current> gdb ./x

GNU gdb 6.6

Copyright (C) 2006 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB.  Type "show warranty" for details.

This GDB was configured as "x86_64-suse-linux"...

Using host libthread_db library "/lib64/libthread_db.so.1".

(gdb) b main

Breakpoint 1 at 0x4005dc: file x.cpp, line 6.

(gdb) r

Starting program: /data/hadoop/hadoop-2.4.0/x 

 

Breakpoint 1, main () at x.cpp:6

6               printf("NULL: %d\n", NULL);

(gdb) macro expand NULL

expands to: __null

(gdb) p NULL

No symbol "__null" in current context.

(gdb) ptype NULL

No symbol "__null" in current context.

(gdb) p __null

No symbol "__null" in current context.

(gdb) ptype __null

No symbol "__null" in current context.

(gdb) 

 

gdb的跟踪结果,不难看到NULL的真身是__null,但__null又是什么了?试图从/usr/include中找到答案:

hadoop@VM-39-166-sles10-64:~/current> grep __null /usr/include/*

/usr/include/libio.h:#  define NULL (__null)

hadoop@VM-39-166-sles10-64:~/current> grep __null /usr/include/*/*

/usr/include/asm-i386/vm86.h:   long __null_ds;

/usr/include/asm-i386/vm86.h:   long __null_es;

/usr/include/asm-i386/vm86.h:   long __null_fs;

/usr/include/asm-i386/vm86.h:   long __null_gs;

/usr/include/asm-i386/vm86.h:   long __null_ds;

/usr/include/asm-i386/vm86.h:   long __null_es;

 

未能找到满意的答案,那么__null只能是g++内置定义的,所以未出现在任何头文件中,事实证明也如此,在代码中可以直接使用__null(尽管如此,但这个不是个好主意):

#include 

#include 

 

int main()

{

        printf("NULL: %d\n", NULL);

        printf("NULL: %d\n", __null);

        printf("sizeof(NULL): %d\n", sizeof(NULL));

        printf("typeid(__null).name(): %s\n", typeid(__null).name());

        printf("typeid(0).name(): %s\n", typeid(0).name());

        return 0;

}

 

 

执行man g++,然后搜索“__null”,找到一个有关的条目:

-Wstrict-null-sentinel (C++ only)

   Warn also about the use of an uncasted "NULL" as sentinel.  When compiling only with GCC this is a valid sentinel, as "NULL" is defined to "__null".  Although it is a null pointer constant not a null pointer, it is guaranteed to of the same size as a pointer(保证和指针相同的大小).  But this use is not portable across different compilers.

 

搜索gcc 4.8.2源码中的FSFChangeLog.11文件:

Wed Aug  7 14:10:07 1996  Jason Merrill  

        * ginclude/stddef.h (NULL): Use __null for G++.

 

继续搜索gcc源代码,可以得到更多信息:

VM-39-166-sles10-64:/data/gcc-4.8.2/gcc # grep __null */*

c-family/c-common.c:  { "__null",               RID_NULL,       0 },

c-family/c-common.c:  /* Create the built-in __null node.  It is important that this is

c-family/c-common.c:      /* Although __null (in C++) is only an integer we allow it

c-family/c-common.h:/* The node for C++ `__null'.  */

cp/call.c:            /* If __null has been converted to an integer type, we do not

cp/NEWS:   is now defined as __null, a magic constant of type (void *)

cp/parser.c:     __null

cp/parser.c:      /* The `__null' literal.  */

 

进一步查看c-family/c-common.c文件:

const struct c_common_resword c_common_reswords[] =

{

  { "__is_union",       RID_IS_UNION,   D_CXXONLY },

  { "__label__",        RID_LABEL,      0 },

  { "__null",           RID_NULL,       0 },

  { "__real",           RID_REALPART,   0 },

  { "__real__",         RID_REALPART,   0 },

  { "__restrict",       RID_RESTRICT,   0 },

 

结构体c_common_resword的定义在c-family/c-common.h中:

/* The node for C++ `__null'.  */

#define null_node                       c_global_trees[CTI_NULL]

 

/* An entry in the reserved keyword table.  */

struct c_common_resword

{

  const char *const word;

  ENUM_BITFIELD(rid) const rid : 16;

  const unsigned int disable   : 16;

};

 

enum c_tree_index

{

    。。。。。。

    CTI_VOID_ZERO,

    CTI_NULL,

    CTI_MAX

};

 

查看文件cp/parser.cparser.cC/C++语法解析器的实现文件:

static tree

cp_parser_primary_expression (cp_parser *parser,

                              bool address_p,

                              bool cast_p,

                              bool template_arg_p,

                              bool decltype_p,

                              cp_id_kind *idk)

    。。。。。。

    case CPP_KEYWORD:

      switch (token->keyword)

        {

          /* These two are the boolean literals.  */

        case RID_TRUE:

          cp_lexer_consume_token (parser->lexer);

          return boolean_true_node;

        case RID_FALSE:

          cp_lexer_consume_token (parser->lexer);

          return boolean_false_node;

 

          /* The `__null' literal.  */

        case RID_NULL:

          cp_lexer_consume_token (parser->lexer);

          return null_node;

 

          /* The `nullptr' literal.  */

        case RID_NULLPTR:

          cp_lexer_consume_token (parser->lexer);

          return nullptr_node;

 

优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/497438

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。