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. Javaee 方法的格式和注意事项

    1.构造方法的格式是什么?有哪些注意事项?   修饰符+方法名称+(参数列表),构造的方法没有返回值,方法名称要和类名一样,有属性参数的需要在成员变量前加this,参数列表的值要和指定的方法格式相同. ...

  2. 【数据分析学习】Pandas思维导图

    点我查看原版

  3. python面向对象三大特性之一封装

    一.什么是封装 在程序设计中,封装(Encapsulation)是对具体对象的一种抽象,即将某些部分隐藏起来,在程序外部看不到,其 含义是其他程序无法调用. 要了解封装,离不开“私有化”,就是将类或者 ...

  4. 漫谈 Google 的 Native Client(NaCl) 技术(二)---- 技术篇(兼谈 LLVM)

    转自:http://hzx5.blog.163.com/blog/static/40744388201172531637729/ 漫谈 Google 的 Native Client(NaCl) 技术( ...

  5. 关于linux系统的sendmail使用中的问题与解决

    系统环境是ubuntu16.04 安装 sudo apt-get install sendmail 检查运行情况 sudo service sendmail status 结果如下,表示正在运行 ● ...

  6. [洛谷 P1967] 货车运输 (最大生成树 lca)

    题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...

  7. maven 依赖的传递性

    1.如图我们有三个项目,项目Age,项目Bge,项目Cge 2.我们使Age项目依赖到Bge项目,Bge项目依赖到Cge项目 Age项目和Bge项目分别执行命令:mvn install  打包*.ja ...

  8. Anaconda安装tensorflow遇到的问题(转)

    tensorflow安装教程 1.找不到Anaconda Prompt 其他教程中说AnacondaPrompt在安装的Anaconda文件夹下面(如D:/Anaconda),但是我在安装中没有找到, ...

  9. struct 模块解决 TCP黏包问题

    首先来看一下产生黏包现象的一段代码: # server.py 服务端 import socket ​ sk = socket.socket() sk.bind(('127.0.0.1',9000)) ...

  10. java陷阱之spring事物未提交和回滚导致不可预知问题

    案发现场 //防止全局配置了 所以这里定义sprnig 不托管事物 @Transactional(propagation = Propagation.NOT_SUPPORTED) public boo ...