当前位置:网站首页>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 ;
边栏推荐
- Realize the function of verifying whether the user has completed login when browsing the page
- Wood board ISO 5660-1 heat release rate mapping test
- xlrd常见操作
- 校招期间 准备面试算法岗位 该怎么做?
- DBeaver同时执行多条insert into报错处理
- 1.2 download and installation of the help software rstudio
- Xlrd common operations
- Summary of data analysis steps
- 【案例】元素的显示与隐藏的运用--元素遮罩
- Some things make feelings nowhere to put
猜你喜欢

Xlrd common operations

MySQL deep paging optimization with tens of millions of data, and online failure is rejected!

基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session

Parker driver maintenance COMPAX controller maintenance cpx0200h

2.2 basic grammar of R language

校招期间 准备面试算法岗位 该怎么做?

leetcode:1139. The largest square bounded by 1

Zhang Lijun: penetrating uncertainty depends on four "invariants"

"Grain mall" -- Summary and induction

Reading and writing operations of easyexcel
随机推荐
2.2.5 basic sentences of R language drawing
Cold violence -- another perspective of objective function setting
Determine the best implementation of horizontal and vertical screens
vant 源码解析之 utils/index.ts 工具函数
Golang (1) | from environmental preparation to quick start
面试官:并发编程实战会吗?(线程控制操作详解)
Influence of oscilloscope probe on signal source impedance
终端安全能力验证环境搭建和渗透测试记录
Problems encountered in office--
xlrd常见操作
R language learning notes
MySQL InnoDB Architecture Principle
Parker驱动器维修COMPAX控制器维修CPX0200H
XML modeling
Some things make feelings nowhere to put
Get JS of the previous day (timestamp conversion)
Wood board ISO 5660-1 heat release rate mapping test
Simple interest mode - lazy type
MQ----activeMq
ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图