NULL

In C

A null-pointer constant is an integral constant expression that evaluates to zero (like 0 or 0L), or the cast of such value to type void* (like (void*)0).

In C++98

A null-pointer constant is an integral constant expression that evaluates to zero (such as 0 or 0L).

In C++11

A null-pointer constant is either an integral constant expression that evaluates to zero (such as 0 or 0L), or a value of type nullptr_t (such as nullptr).

From the description of cpluscplus reference, we can see that NULL's definition is compiler dependent. In this blog, I only discuss the behavior of gcc in linux operating system.

// file test.c
#include<stdio.h> void func(int* p) {
if (p) *p = 1;
} int main()
{
int* p = 0;//NULL;
func(NULL); return 0;
}

use gcc to see what's NULL is:

gcc -E test.c | less

result:

# 2 "test.c" 2

void func(int* p) {
if (p) *p = 1;
} int main()
{
int* p = 0;
func(((void *)0)); return 0;
}

we can see that in C, NULL is (void*)0;

In C++

#include<iostream>

void func(int* p) {
if (p) *p = 1;
} int main()
{
int* p = 0;//NULL;
func(NULL);
std::cout << p << std::endl; return 0;
}

result:

void func(int* p) {
if (p) *p = 1;
} int main()
{
int* p = 0;
func(__null);
std::cout << p << std::endl; return 0;
}

it's __null, a integral constant expression.

The only change that might affect people is the type of NULL: while it is required to be a macro, the definition of that macro is not allowed to be (void*)0, which is often used in C.

For g++, NULL is #define'd to be __null, a magic keyword extension of g++.

The biggest problem of #defining NULL to be something like “0L” is that the compiler will view that as a long integer before it views it as a pointer, so overloading won't do what you expect. (This is why g++ has a magic extension, so that NULL is always a pointer.)

nullptr

typedef decltype(nullptr) nullptr_t;
Null pointer type (C++)
Type of the null pointer constant nullptr. This type can only take one value: nullptr, which when converted to a pointer type takes the proper null pointer value. Even though nullptr_t it is not a keyword, it identifies a distinct fundamental type: the type of nullptr. As such, it participates in overload resolution as a different type. This type is only defined for C++ (since C++11).

0-NULL-nullptr的更多相关文章

  1. 语法:c++对关于空指针0/NULL/nullptr三者的演变

    来源: https://blog.csdn.net/u010558281/article/details/77793644 字面意义上的解释: 0:整型常量 NULL:预处理符号 nullptr:空指 ...

  2. php中0," ",null和false的区别

    php中很多还不懂php中0,"",null和false之间的区别,这些区别有时会影响到数据判断的正确性和安全性,给程序的测试运行造成很多麻烦.先看一个例子: <? $str ...

  3. 0,null,empty,空,false,isset

    <?php header("Content-type: text/html; charset=utf-8"); $a=0; //1. if($a==0) { echo $a; ...

  4. '0','\0',NULL,EOF的区别

    要看是不是一个东西,打印一下即可 printf("%d %d %d %d\n",'0','\0',NULL,EOF); 输出: 48 0 0 -1 结论: '\0'与NULL 都是 ...

  5. 0,null,undefined,[],{},'',false之间的关系

    0与一些虚值的比较: 0与false 0==false true 0与'': =='' true 0与[]: ==[] true 0与NaN: 0==NaN false 0与undefined 0== ...

  6. myBatis-plus异常提示For input string: "{0=null}"

    异常信息 org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Per ...

  7. php 0,null,empty,空,false,字符串关系(转)

    在php中由于是弱类型语言,不同类型值之间可以隐式转换,使得false,null,”,0,’0′这几个值的比较有些混乱,现总结一下: //相等判断 '' == NULL == 0 == false ( ...

  8. 0,'0','\0',NULL的区别

    0,'0','\0',NULL的区别 1,0是一个值,可以是char ,int ,float,double等类型: 2,'0'是一个字符(char)类型,它的ASCII码值是48: 3,'\0'也是一 ...

  9. 空指针/0/NULL

    空指针/0/NULL 空指针是一个被赋值为0的指针,在没有被具体初始化之前,其值为0. NULL 是一个标准规定的宏定义,用来表示空指针常量. #define NULL 0   或者 #define ...

  10. 你所不知道的 JS: null , undefined, NaN, true==1=="1",false==0=="",null== undefined

    1 1 1 === 全相等(全部相等) ==  值相等(部分相等) demo: var x=0; undefined var y=false; undefined if(x===y){ console ...

随机推荐

  1. (转)Bootstrap 之 Metronic 模板的学习之路 - (6)自定义和扩展

    https://segmentfault.com/a/1190000006815041 前面我们将 Metronic 的结构和源码大致浏览了一遍,Metronic 整个文件包有三百多兆,在实际项目中, ...

  2. Period UVA - 1328_结论题

    Code: #include<cstdio> #include<cstring> using namespace std; const int maxn=1000000+5; ...

  3. Project Euler 38 Pandigital multiples

    题意: 将192分别与1.2.3相乘: 192 × 1 = 192192 × 2 = 384192 × 3 = 576 连接这些乘积,我们得到一个1至9全数字的数192384576.我们称192384 ...

  4. JavaScript基础简介

    JavaScript引入的方式 直接在<script>标签中写 <script> console.log('hello world!'); </script> 引入 ...

  5. VR开发2015年终总结

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50617605 作者:car ...

  6. android 异常解决方案汇总

    1)异常:Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法) 1.在工程下新建lib文件夹,将需要的第三方包拷贝进来. 2.将引用的第三方 ...

  7. webKit 内核浏览器 源码分析

    如需转载,请注明出处! WebSite: http://www.jjos.org/ 作者: 姜江 linuxemacs@gmail.com QQ: 457283 这是一篇自己写于一年前的工作文档,分享 ...

  8. 关系数据库标准语言SQL

    篇幅过长,恐惧者慎入!!!基础知识,大神请绕道!!! 本节要点: l  SQL概述 l  学生-课程关系 l  数据定义 基本表的定义.删除与修改 索引的建立与删除 l  查询 单表查询 连接查询 嵌 ...

  9. BA-siemens-apogee自适应控制

    简介 APOGEE楼控系统的控制器中,包括了由 Cybosoft开发的基于无模型自适应控制技术的自适应控制. 自适应控制是一个复杂的闭环循环控制算 法.自适应控制能自动校正参数以补偿机械的系 统/负载 ...

  10. codevs——T1048 石子归并

     http://codevs.cn/problem/1048/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Descriptio ...