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. Kafka学习笔记(4)----Kafka的Leader Election

    1. Zookeeper的基本操作 zookeeper中的节点可以持久化/有序的两个维度分为四种类型: PERSIST:持久化无序(保存在磁盘中) PERSIST_SEQUENTIAL:持久化有序递增 ...

  2. Python 3 print 函数用法总结

    Python 3 print 函数用法总结 1. 输出字符串和数字 print("runoob")    # 输出字符串 runoob print(100)            ...

  3. Django 了解

    Django是一个开放源代码的Web应用框架 Django也是一个基于 MVC 构造的框架. 但是在Django中,控制器接受用户输入的部分由框架自行处理,所以 Django 里更关注的是模型(Mod ...

  4. CentOS7环境RabbitMQ集群配置管理(转载)

    CentOS7环境RabbitMQ集群配置管理(转载)   CentOS7系统内核版本:3.10.0-514.26.2.el7.x86_64 一.对应主机host地址(三台主机host文件要保持一致) ...

  5. SQL表查询

    CREATE TABLE student( Sno ) NOT NULL PRIMARY KEY, Sname ) NOT NULL, Ssex ) NOT NULL, Sbirthday DATET ...

  6. Tjoi2019 甲苯先生和大中锋的字符串 后缀自动机_差分

    tjoi胆子好大,直接出了两道送分题...... 都 9102 年了,还有省选出模板题QAQ...... Code: #include <bits/stdc++.h> #define se ...

  7. android Build系统

    http://www.ibm.com/developerworks/cn/opensource/os-cn-android-build/ android Build系统 超链接

  8. Gradle学习总结——抓重点学Gradle

    前言 网上关于Gradle的教程很多,但很多都是以"面"切入- 通过大量讲解其用法及其API分类来阐述.但Gradle API使用技巧众多,API更是成千上百,臣妾记不住呀.个人深 ...

  9. ActiveMQ 发送和接收消息

    一.添加 jar 包 <dependency> <groupId>org.apache.activemq</groupId> <artifactId>a ...

  10. 教你十分钟构建好 SpringBoot + SSM 框架

    目前最主流的 java web 框架应该是 SSM,而 SSM 框架由于更轻便与灵活目前受到了许多人的青睐.而 SpringBoot 的轻量化,简化项目配置, 没有 XML 配置要求等优点现在也得到了 ...