当前位置:网站首页>Pointer parameter passing vs reference parameter passing vs value parameter passing
Pointer parameter passing vs reference parameter passing vs value parameter passing
2022-07-05 21:35:00 【MrL_ JJ】
List of articles
Preface
Have you ever encountered such a dilemma , Calling someone else's api when , Whether it's value transfer , The reference , Transmission pointer , It's easy to use , But I'm making it myself api when , But I got stuck here. I should adopt pointer to pass parameters , Value pass parameter or reference ? This dilemma of uncertain choices .
One 、 Pointer and reference are different
The pointer : Pointer is also a variable , What is stored is the address of other variables , So a pointer accessing a variable is an indirect access . A pointer is an entity , Memory space needs to be allocated . Pointers don't have to be initialized when they are defined , And the space that points to is variable . Pointer self increment operation , The result pointer points to the next space .
quote : References are just aliases for variables , There is no need to allocate memory space . Reference access to a variable is a direct access to . References must be initialized when they are defined , And it can't change . The underlying reference is implemented through pointers ; Quote autoincrement , The result is the quoted variable value plus 1
It's also different as a parameter , The essence of passing a pointer is to pass a value , The value passed is the address of the pointer ; The essence of transmission reference is address , Passing the address of the variable , So when should I pass the pointer , When will it be transmitted and quoted ?
Two 、 For functions that do not modify the incoming parameters
1. If the data object is small , Such as built-in data types or small structures , Is passed by value ;
2. If the data object is an array , Then use the pointer ( The only choice ), And the pointer is declared as pointing const The pointer to ;
3. If the data object is a large structure , Then use const Pointer or reference , The efficiency of the procedure has been improved . This can save the time and space required by the structure ;
4. If the data object is a class object , Then use const quote ( The standard way to pass class object parameters is by reference );
3、 ... and 、 For functions that modify incoming parameters
1、 If the data is a built-in data type , Then use the pointer
2、 If the data object is an array , Only pointers can be used
3、 If the data object is a structure , Use a reference or pointer
4、 If the data is a class object , Use reference
One last word : Pointer is not type safe ( There are more references than pointers, type checking ) It's best to do type checking before using pointers , Prevent the appearance of wild pointer ;
边栏推荐
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- Dictionary tree simple introductory question (actually blue question?)
- 2.2 basic grammar of R language
- Influence of oscilloscope probe on measurement bandwidth
- Clion-MinGW编译后的exe文件添加ico图标
- 面试官:并发编程实战会吗?(线程控制操作详解)
- Environment configuration problem record
- Selenium gets the verification code image in DOM
- MySQL 千万数据量深分页优化, 拒绝线上故障!
- Four components of logger
猜你喜欢

LeetCode_哈希表_困难_149. 直线上最多的点数

Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)

1.2 download and installation of the help software rstudio
![Longest swing sequence [greedy practice]](/img/e1/70dc21b924232c7e5e3da023a4bed2.png)
Longest swing sequence [greedy practice]

"Grain mall" -- Summary and induction

递归查询多级菜单数据

Influence of oscilloscope probe on signal source impedance

What should I do to prepare for the interview algorithm position during school recruitment?

R language learning notes

Introduction of ArcGIS grid resampling method
随机推荐
Selenium gets the verification code image in DOM
GCC9.5离线安装
2.2.3 output of documents
JS common method encapsulation
js常用方法封装
sql常用语法记录
Simple interest mode - lazy type
Aitm 2-0003 horizontal combustion test
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
Postgres establish connection and delete records
Teach yourself to train pytorch model to Caffe (2)
Teach yourself to train pytorch model to Caffe (I)
Selenium's method of getting attribute values in DOM
Chap2 steps into the palace of R language
Learning notes of statistical learning methods -- Chapter 1 Introduction to statistical learning methods
Chapter 05_ Storage engine
selenium 获取dom内属性值的方法
SYSTEMd resolved enable debug log
100 cases of shell programming
校招期间 准备面试算法岗位 该怎么做?