NULL

In C

  1. 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

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

In C++11

  1. 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.

  1. // file test.c
  2. #include<stdio.h>
  3. void func(int* p) {
  4. if (p) *p = 1;
  5. }
  6. int main()
  7. {
  8. int* p = 0;//NULL;
  9. func(NULL);
  10. return 0;
  11. }

use gcc to see what's NULL is:

  1. gcc -E test.c | less

result:

  1. # 2 "test.c" 2
  2. void func(int* p) {
  3. if (p) *p = 1;
  4. }
  5. int main()
  6. {
  7. int* p = 0;
  8. func(((void *)0));
  9. return 0;
  10. }

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

In C++

  1. #include<iostream>
  2. void func(int* p) {
  3. if (p) *p = 1;
  4. }
  5. int main()
  6. {
  7. int* p = 0;//NULL;
  8. func(NULL);
  9. std::cout << p << std::endl;
  10. return 0;
  11. }

result:

  1. void func(int* p) {
  2. if (p) *p = 1;
  3. }
  4. int main()
  5. {
  6. int* p = 0;
  7. func(__null);
  8. std::cout << p << std::endl;
  9. return 0;
  10. }

it's __null, a integral constant expression.

  1. 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.
  2. For g++, NULL is #define'd to be __null, a magic keyword extension of g++.
  3. 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

  1. typedef decltype(nullptr) nullptr_t;
  2. Null pointer type (C++)
  3. Type of the null pointer constant nullptr.
  4. This type can only take one value: nullptr, which when converted to a pointer type takes the proper null pointer value.
  5. 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.
  6. 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. MySQL_基本操作

    sql语句 Sql语句主要用于存取数据,查询数据,更新数据和管理数据库系统. #Sql语句分为3种类型 #1.DDL语句:数据库定义语言: 数据库.表.视图.索引.存储过程,例如CREATE DROP ...

  2. 安装`lrzsz`包及其报错解决办法

    rz命令的安装包名是lrzsz. 安装lrzsz包时报错Failed to mount cd:///?devices=/dev/sr1,/dev/sr0 on /var/adm/mount/AP_0x ...

  3. a标记地址的几种用法

    1.<a href="tel://号码"></a> 手机使用能自动拨打电话 //可以省略 2.<a href="mailto://邮箱&qu ...

  4. selenium的显示等待和隐式等待区别

    1.selenium的显示等待 原理:显式等待,就是明确的要等到某个元素的出现或者是某个元素的可点击等条件,等不到,就一直等,除非在规定的时间之内都没找到,那么就跳出Exception.(简而言之:就 ...

  5. vue开发基本步骤

    1      安装node.js 安装node.js之前,先进行nvm的安装: https://github.com/coreybutler/nvm-windows/releases最好选择稳定版本 ...

  6. 洛谷P1046 陶陶摘苹果

    题目描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出 101010 个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个 303030 厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳 ...

  7. LIS,LCS,LICS 学习笔记

    1.最长上升子序列(LIS) 子序列: 1.可以不连续 2.相对位置不变 dp[i][j] 表示前i位置,最大值为j的LIS长度 1. dp[i-1][j] 前i-1位置,最大值为j的LIS长度 (没 ...

  8. SQLSERVER数据库还原的时候,报 WITH MOVE 子句可用于重新定位一个或多个文件 的错误,求解决

    http://www.flybi.net/question/4070 梁勇 - 天善智能微软BI首席讲师 数据库备份文件还原产生这个错误的原因是:还原目录下存在多个同名文件, 如图所示,只需要将第2个 ...

  9. Manarcher 求 字符串 的最长回文子串 【记录】

    声明:这里仅仅写出了实现过程.想学习Manacher的能够看下这里给出的实现过程,算法涉及的一些原理推荐个博客. 给个链接 感觉讲的非常细 引子:给定一个字符串s,让你求出最长的回文子串的长度. 算法 ...

  10. oracle rac下调节redo log file 文件大小

    rac下调节redo log file 文件大小 (1)查看当前日志信息: select * from v$logfile; (步骤2中得路径能够在这里MEMBER列看到,redo文件名称自己命名.比 ...