当前位置:网站首页>野指针,空指针,失效指针
野指针,空指针,失效指针
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);
边栏推荐
- 2022-07-22 mysql/stonedb并行hashJoin内存占用分析
- Lambda expressions sort list objects in multiple fields
- Don't care too much about what others think of you
- Use dichotomy to find specific values from the array
- 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
- (note sorting is not completed) [graph theory: find the shortest path of single source]
- The function of extern, static, register, volatile keywords in C language; Nanny level teaching!
- reflex
- Redis fragment cluster
- STM32 external interrupt (register version)
猜你喜欢

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

在线问题反馈模块实战(十二):实现图片删除功能

C language to achieve three chess? Gobang? No, it's n-chess

MGR_ mysqlsh_ Keepalive high availability architecture deployment document

Practice of online problem feedback module (12): realize image deletion function

STM32基于hal库的adc以DMA的多通道采样以及所遇问题解决

STM32H750VBT6驱动程控增益放大模块PGA113——基于CubeMX的Hal库

Hackingtool of security tools

第二部分—C语言提高篇_2. 内存分区

OWASP TOP10 penetration test
随机推荐
sojson jsjiami.com. V6 crawler JS reverse
安全工具之hackingtool
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
MongoDB应用场景及选型(海量数据存储选型)
第二部分—C语言提高篇_2. 内存分区
Libevent multithreaded server + client source code
Can you increase muscle without exercise??? Just get an injection of hibernating black bear serum
Three level classification / menu query tree structure
Introduction to pyqt5 - student management system
【LeetCode】444. 序列重建
Can recursion still play like this? Recursive implementation of minesweeping game
【行测】图形找规律类题目
owasp top10 渗透测试
Talk about your thoughts about the future
Thinking of data analysis -- analyzing the retail industry as a whole -- an all-round and multifaceted detailed analysis
Tensorflow Einstein function
聚合型新生态模式-分享购,会员及奖励制度
Redis fragment cluster
OWASP TOP10 penetration test
avaScript的流程控制语句