C++ NULL与nullptr的区别
在C里面,由于处处都要使用指针,所以导致NULL遍布各地。我们先来看C99是怎么定义NULL的:
NULL can be defined as any null pointer constant. Thus existing code can retain definitions of
NULL as 0 or 0L, but an implementation may also choose to define it as (void*)0. This latter
form of definition is convenient on architectures where sizeof(void*) does not equal the
size of any integer type. It has never been wise to use NULL in place of an arbitrary pointer as a
function argument, however, since pointers to different types need not be the same size. Thelibrary avoids this problem by providing special macros for the arguments to signal, the one
library function that might see a null function pointer.
A null pointer constant is an integer literal (2.13.2) with value zero or a prvalue of type std::nullptr_t.
A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type
and is distinguishable from every other value of object pointer or function pointer type. Such a conversion
is called a null pointer conversion. Two null pointer values of the same type shall compare equal. The
conversion of a null pointer constant to a pointer to cv-qualified type is a single conversion, and not the
sequence of a pointer conversion followed by a qualification conversion (4.4). A null pointer constant of
integral type can be converted to a prvalue of type std::nullptr_t. [ Note: The resulting prvalue is not a
null pointer value. —end note ]
A prvalue of type “pointer to cv T,” where T is an object type, can be converted to a prvalue of type “pointer
to cv void”. The result of converting a non-null pointer value of a pointer to object type to a “pointer to
cv void” represents the address of the same byte in memory as the original pointer value. The null pointer
value is converted to the null pointer value of the destination type.
A prvalue of type “pointer to cv D”, where D is a class type, can be converted to a prvalue of type “pointer
to cv B”, where B is a base class (Clause 10) of D. If B is an inaccessible (Clause 11) or ambiguous (10.2)
base class of D, a program that necessitates this conversion is ill-formed. The result of the conversion is a
pointer to the base class subobject of the derived class object. The null pointer value is converted to the
null pointer value of the destination type.
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
#include <iostream>
#include <algorithm>
#include <memory>
void fun(int)
{
std::cout << "fuck1" << std::endl;
}
void fun(void *)
{
std::cout << "fuck2" << std::endl;
}
int main(int argc, char *argv[])
{
fun(NULL);
system("pause");
return ;
}
int main(int argc, char *argv[])
{
fun(nullptr);
system("pause");
return ;
}
struct Fuck
{
Fuck(char *){ }
};
int main(int argc, char *argv[])
{
auto p = std::make_shared<Fuck>(NULL);
throwing();
system("pause");
return ;
}
template<class _Ty,
class... _Types> inline
shared_ptr<_Ty> make_shared(_Types&&... _Args)
{ // make a shared_ptr
_Ref_count_obj<_Ty> *_Rx =
new _Ref_count_obj<_Ty>(_STD forward<_Types>(_Args)...);
shared_ptr<_Ty> _Ret;
_Ret._Resetp0(_Rx->_Getptr(), _Rx);
return (_Ret);
}
// TEMPLATE CLASS _Ref_count_obj
template<class _Ty>
class _Ref_count_obj
: public _Ref_count_base
{ // handle reference counting for object in control block, no allocator
public:
template<class... _Types>
_Ref_count_obj(_Types&&... _Args)
: _Ref_count_base()
{ // construct from argument list
::new ((void *)&_Storage) _Ty(_STD forward<_Types>(_Args)...);
}
_Ty *_Getptr() const
{ // get pointer
return ((_Ty *)&_Storage);
}
private:
virtual void _Destroy() _NOEXCEPT
{ // destroy managed resource
_Getptr()->~_Ty();
}
virtual void _Delete_this() _NOEXCEPT
{ // destroy self
delete this;
}
typename aligned_union<, _Ty>::type _Storage;
};
auto p = std::make_shared<Fuck>(nullptr);
C++ NULL与nullptr的区别的更多相关文章
- (转)null和NULL和nullptr和””区别
突然想到这个有趣的问题:C语言和C++对大小写是敏感的,也就是说null和NULL是区别对待的.NULL代表空地址,null只是一个符号.便来深究,看了很多资料,总结如下: 其实null和NULL都是 ...
- NULL和nullptr的区别
//error C2665: “go”: 2 个重载中没有一个可以转换所有参数类型 #include <iostream> void go(int num) { std::cout < ...
- 【转载】C/C++杂记:NULL与0的区别、nullptr的来历
原文:C/C++杂记:NULL与0的区别.nullptr的来历 某些时候,我们需要将指针赋值为空指针,以防止野指针. 有人喜欢使用NULL作为空指针常量使用,例如:int* p = NULL;. ...
- 再谈NULL和nullptr(C++11)区别
在谈NULL和nullptr区别之前,我们先看段代码: #include "stdafx.h" #include <iostream> using namespace ...
- 字符串怎么换行 || 字符串中使用单引号时应该怎么写 || 保留两位小数 || 数字0在if中的意思是false || 什么情况下会会报undefined || null和undefined的区别 ||
换行的字符串 "This string\nhas two lines" 字符串中使用单引号时应该怎么写 'You\'re right, it can\'t be a quote' ...
- MySQL 中NULL和空值的区别
平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...
- tips null和undefined的区别
tips null和undefined的区别 1.undefined类型 undefined类型只有一个值,即特殊的undefined.在使用var声明变量但未对其加以初始化时,这个变量的值就是und ...
- Convert和Parse对null值处理的区别
类型的转换在日常的变成中是经常被用到的,我们最常用的类型转换的方法就是Convert和Parse, 下面来说一下这两者null值处理的区别. int i1 = Convert.ToInt32(null ...
- [BS-22] Objective-C中nil、Nil、NULL、NSNull的区别
Objective-C中nil.Nil.NULL.NSNull的区别 1.定义: nil: OC语言定义:#define nil __DARWIN_NULL / #define __D ...
随机推荐
- 51nod1107(逆序对数&归并排序)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1107 题意:中文题诶- 思路:通过题意可以发现对于两点p1(x ...
- Luogu P3065 [USACO12DEC]第一!First!【字典树/拓扑排序】By cellur925
题意:给你许多字符串,你可以改变字母序大小,问有哪些字符串可能成为字典序最小的字符串. 我们考虑把这些字符串都塞到\(trie\)树上.之后检索每一个字符串的时候,我们看和他同一层的地方是否有字符,如 ...
- Glassfish 设置时区
对于Glassfish domain 或者instance下,某个日志的时区不对,前提是系统时区争取. 可以尝试通过如下命令查看jvm 时区设置 asadmin list-jvm-options 如果 ...
- Jar命令用法
JAR文件 JAR文件 全称:Java Archive File , 意思是Java档案文件.通常JAR文件是一种压缩文件,与常见的ZIP压缩文件兼容,通常被称为JAR包. JAR文件和ZIP文件的区 ...
- DRF教程8-过滤
在写后端api时,经常需要使用各种过滤条件,可以使用Q对查询集进行过滤,这里介绍一个新玩意儿 以下是基础文档 https://django-filter.readthedocs.io/en/maste ...
- 遇到的一些vue的问题
一.事件绑定中的事件处理方法后加不加括号问题 1.例如: click事件后加不加括号 a.@click = "getContent" b.@click = "getCon ...
- ms sqlserver 清除数据库日志脚本
USE [master] GO ALTER DATABASE F360DW SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE F360DW SET ...
- 《java学习二》jvm性能优化-----认识jvm
Java内存结构 Java堆(Java Heap) java堆是java虚拟机所管理的内存中最大的一块,是被所有线程共享的一块内存区域. 在虚拟机启动时创建.此内存区域的唯一目的就是存放对象实例,这一 ...
- 表单验证插件jquery.validate.js
最常使用JavaScript的场合就是表单的验证,而jQuery作为一个优秀的JavaScript库,也提供了一个优秀的表单验证插件----Validation.Validation是历史最悠久的jQ ...
- HTTP1.1中CHUNKED编码解析(转载)
HTTP1.1中CHUNKED编码解析 一般HTTP通信时,会使用Content-Length头信息性来通知用户代理(通常意义上是浏览器)服务器发送的文档内容长度,该头信息定义于HTTP1.0协议RF ...