当前位置:网站首页>野指针,空指针,失效指针
野指针,空指针,失效指针
2022-07-24 06:29:00 【张淑芬~】
1.野指针和空指针
把这两个放在一起,是因为这个实在是没有什么重要的知识点,还是先看一下吧。
int main()
{
int *p;
int *p1=nullptr;
return 0;
}指针p为野指针,也就是说在定义一个指针时没有初始化为nullptr,那么这个指针会指向一个随机地址,这就是野指针,野指针是非常危险的。
p1在我们定义的时候初始化为空,p1就是一个空指针。
注意我这里用的是nullptr,不是NULL,nullptr在C++中是专门为了初始化或赋值一个指针为空时用的关键字,为什么不用NULL呢?这里简单说一下,本专栏后期会专门有一期文章介绍C++中nullptr。
在C语言编译器中,NULL的定义为#define NULL 0或者#define NULL (void*)0;NULL是根据宏定义的字面常量0或者无类型指针0常量,那么这种情况有时候会导致二义性,这里就不举例子了,后期文章可以仔细去看一下,为了解决这种情况,在C11中标准中,处于兼容性的考虑,字面常量0的二义性并没有消除,但标准还是为二义性给出了新的答案,就是nullptr(一个所谓“指针空值类型”的常量)。
2.失效指针
失效指针是本章的一个重点,那么什么是失效指针呢?我们来举例说明:
int main()
{
int *r=(int *)malloc(sizeof(int));
*r=100;
free(r);
边栏推荐
- 【学习笔记】从汇编看 a+++a与 a+a++的区别
- STM32基于hal库的adc以DMA的多通道采样以及所遇问题解决
- 17. 什么情况用ArrayList or LinkedList呢?
- Do you really know the judgement sentence?
- 先爱自己,再爱别人。
- XXL execute node error log swiping
- Design a function print to print the string. If only the string parameter s is passed, the string length is compared with 10, greater than 10, print the first 10 characters, less than 10, and output a
- 别太在意别人的眼光,那会抹杀你的光彩
- 上传excel文件
- Three level classification / menu query tree structure
猜你喜欢

华为专家自述:如何成为优秀的工程师

tensorflow einsum函数

owasp top10 渗透测试

It can be written in 10 minutes -- 25~30k foreign enterprise recruitment interview questions, isn't it easy~
![(note sorting is not completed) [graph theory: find the shortest path of single source]](/img/58/e61aea3c4d0a33d3615144763160f7.png)
(note sorting is not completed) [graph theory: find the shortest path of single source]

第二部分—C语言提高篇_4. 二级指针

Redis persistence

Three level classification / menu query tree structure

Create WPF project

OWASP TOP10 penetration test
随机推荐
Record the pits encountered in the deserialization of phpserializer tool class
Job search memo
传统电商红利消失,怎么进军新型社交电商?
找工作备忘
第二部分—C语言提高篇_4. 二级指针
ADB interaction - kill the ugly default shell interface
Nodejs enables multi process and inter process communication
[learning notes] what happens when the URL is input into the page presentation?
Practice of online problem feedback module (12): realize image deletion function
Never lose yourself!
Geek planet ByteDance one stop data governance solution and platform architecture
三级分类/菜单的查询——树形结构
Use of redis
处理树形结构数据
tensorflow scatter_ Nd function
Redis distributed cache learning notes
Sealos packages and deploys kubesphere container platform
STM32外部中断(寄存器版本)
Who you are is up to you!
C language to achieve three chess? Gobang? No, it's n-chess