当前位置:网站首页>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 ;
边栏推荐
- 终端安全能力验证环境搭建和渗透测试记录
- 秋招将临 如何准备算法面试、回答算法面试题
- Generics of TS
- Chapter 05_ Storage engine
- Clion-MinGW编译后的exe文件添加ico图标
- Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
- Robot framework setting variables
- KingbaseES V8R3集群维护案例之---在线添加备库管理节点
- Teach yourself to train pytorch model to Caffe (I)
- Deployment of Jenkins under win7
猜你喜欢

Enclosed please find. Net Maui's latest learning resources

Golang(1)|从环境准备到快速上手
![R language [data management]](/img/41/b89bb8794c06280e58988e1c1a5e02.png)
R language [data management]

Interviewer: will concurrent programming practice meet? (detailed explanation of thread control operation)

leetcode:1755. Sum of subsequences closest to the target value

EasyExcel的读写操作

Cold violence -- another perspective of objective function setting

Clion configures Visual Studio (MSVC) and JOM multi-core compilation

ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图

Zhang Lijun: penetrating uncertainty depends on four "invariants"
随机推荐
Enclosed please find. Net Maui's latest learning resources
MMAP
XML modeling
MySQL InnoDB Architecture Principle
one hundred and twenty-three thousand four hundred and fifty-six
EasyExcel的读写操作
DBeaver同时执行多条insert into报错处理
Dictionary tree simple introductory question (actually blue question?)
校招期间 准备面试算法岗位 该怎么做?
Selenium's method of getting attribute values in DOM
Sequence alignment
2022-07-03-CKA-粉丝反馈最新情况
事项研发工作流全面优化|Erda 2.2 版本如“七”而至
Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"
[daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
Add ICO icon to clion MinGW compiled EXE file
1.2 download and installation of the help software rstudio
leetcode:1755. Sum of subsequences closest to the target value
Sophomore personal development summary
Zhang Lijun: penetrating uncertainty depends on four "invariants"