当前位置:网站首页>02-3、指针和引用的区别
02-3、指针和引用的区别
2022-07-17 14:45:00 【列宁格勒的街头】
前言:本文章主要用于个人复习,追求简洁,感谢大家的参考、交流和搬运,后续可能会继续修改和完善。
因为是个人复习,会有部分压缩和省略。
指针和引用的区别
int main()
{
int a = 10;
int& ra = a;//在语法上,这里给a这块空间取了一个别名,没有新开空间
ra = 20;
int* pa = &a;//在语法上,这里定义了一个pa指针变量,开了4个字节存储a的地址
*pa = 20;
return 0;
}实际从汇编实现角度,引用的底层也是类似指针存地址的方式处理

在使用引用和指针时,它们的区别:
1.引用概念上定义一个变量的别名,指针存储一个变量地址。
2.引用再定义时必须初始化,指针没有要求。
3.引用在初始化时引用一个实体后,就不能再引用其他实体,而指针可以在任何时候指向任何一个同类型实体。
4.没有NULL引用,但有NULL指针。
5.在sizeof中含义不同:引用结果为引用类型的大小,但指针始终是地址空间所占字节个数。
6.引用自加即引用的实体增加1,指针自加即指针向后偏移一个类型的大小
7.有多级指针,但是没有多级引用。
8.访问实体方式不同,指针需要显示解引用,引用是编译器自己处理
9.引用比指针使用起来相对更安全
边栏推荐
- Synchronized lock upgrade
- 【多线程】JUC详解 (Callable接口、RenntrantLock、Semaphore、CountDownLatch) 、线程安全集合类面试题
- 华为无线设备配置频谱导航
- To build agile teams, these methods are indispensable
- 解决邮件客户端QQ Mail及Thunderbird无法登入Outlook的问题
- Déléguer un chargeur tel qu'un parent
- Huawei machine test: Message decompression
- Powercli script performance optimization
- Mysql 自增id、uuid与雪花id
- Loj#2324-「清华集训 2017」小 Y 和二叉树
猜你喜欢

今日睡眠质量记录79分

LeetCode 745. Prefix and suffix search

Deep Learning for Generic Object Detection: A Survey-论文阅读笔记

Powercli script performance optimization

NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA

Keras deep learning practice (14) -- r-cnn target detection from scratch

Unity Dropdown(可编辑,可输入)下拉选择框,带文本联想

Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记

传输层 -------- TCP(一)

性能优化之@Contended减少伪共享
随机推荐
Detailed explanation of Euler angle, axis angle, quaternion and rotation matrix
A fastandrobust volutionalneuralnetwork based defect detection model inproductqualitycontrol reading notes
性能优化之@Contended减少伪共享
Tier defect detection using full revolutionary network
Cmake常用命令(五)
Deep learning for generic object detection: a survey
Codeforces - 587e (linear basis + segment tree + difference)
The concept of data guard broker and the configuration process of data guard broker
zabbix代理服务器配置
An error, uncaught typeerror: modalfactory is not a constructor
TS solves the problem that the type file of the imported plug-in does not exist
委派雙親之類加載器
Sword finger offer II 041 Average value of sliding window
Leetcode 1310. 子数组异或查询
At5147-[agc036d]negative cycle [DP, model conversion]
IP SAN has an independent file system. After the application server accesses the IP SAN through the network sharing protocol, it can read and write the files in the file system
JVM hook hooks function
SQL UNION操作符
PowerCLI 脚本性能优化
Dynamic memory allocation problem