From 51754deb59c4aa90e4f9df9bb40fdfc13627f2af Mon Sep 17 00:00:00 2001 From: light-city <455954986@qq.com> Date: Sat, 10 Aug 2019 12:04:20 +0800 Subject: [PATCH] update dir --- README.md | 1 + pointer_refer/README.md | 174 +++++++++++++++++++++++++++++++ pointer_refer/copy_construct | Bin 0 -> 9592 bytes pointer_refer/copy_construct.cpp | 35 +++++++ pointer_refer/effec | Bin 0 -> 20576 bytes pointer_refer/effec.cpp | 22 ++++ 6 files changed, 232 insertions(+) create mode 100644 pointer_refer/README.md create mode 100755 pointer_refer/copy_construct create mode 100644 pointer_refer/copy_construct.cpp create mode 100755 pointer_refer/effec create mode 100644 pointer_refer/effec.cpp diff --git a/README.md b/README.md index f741628..2e99f93 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ - [x] [::那些事](./::) - [x] [enum那些事](./enum) - [x] [decltype那些事](./decltype) +- [x] [引用与指针那些事](./pointer_refer) 代码运行: 全部在linux下用vim编写,使用gcc/g++调试!全部可正常运行! diff --git a/pointer_refer/README.md b/pointer_refer/README.md new file mode 100644 index 0000000..c427168 --- /dev/null +++ b/pointer_refer/README.md @@ -0,0 +1,174 @@ +# 引用与指针那些事 + +## 关于作者: + +个人公众号: + +![](../img/wechat.jpg) + +## 1.引用与指针 + +总论: + +| 引用 | 指针 | +| ------------ | ------------ | +| 必须初始化 | 可以不初始化 | +| 不能为空 | 可以为空 | +| 不能更换目标 | 可以更换目标 | + +> 引用必须初始化,而指针可以不初始化。 + +我们在定义一个引用的时候必须为其指定一个初始值,但是指针却不需要。 + +```c++ +int &r; //不合法,没有初始化引用 +int *p; //合法,但p为野指针,使用需要小心 +``` + +> 引用不能为空,而指针可以为空。 + +由于引用不能为空,所以我们在使用引用的时候不需要测试其合法性,而在使用指针的时候需要首先判断指针是否为空指针,否则可能会引起程序崩溃。 + +```c++ +void test_p(int* p) +{ + if(p != null_ptr) //对p所指对象赋值时需先判断p是否为空指针 + *p = 3; + return; +} +void test_r(int& r) +{ + r = 3; //由于引用不能为空,所以此处无需判断r的有效性就可以对r直接赋值 + return; +} +``` + +> 引用不能更换目标 + +指针可以随时改变指向,但是引用只能指向初始化时指向的对象,无法改变。 + +``` +int a = 1; +int b = 2; + +int &r = a; //初始化引用r指向变量a +int *p = &a; //初始化指针p指向变量a + +p = &b; //指针p指向了变量b +r = b; //引用r依然指向a,但a的值变成了b +``` + +## 2.引用 + +#### [左值引用](https://www.huihut.com/interview/#/?id=%e5%b7%a6%e5%80%bc%e5%bc%95%e7%94%a8) + +常规引用,一般表示对象的身份。 + +#### [右值引用](https://www.huihut.com/interview/#/?id=%e5%8f%b3%e5%80%bc%e5%bc%95%e7%94%a8) + +右值引用就是必须绑定到右值(一个临时对象、将要销毁的对象)的引用,一般表示对象的值。 + +右值引用可实现转移语义(Move Sementics)和精确传递(Perfect Forwarding),它的主要目的有两个方面: + +- 消除两个对象交互时不必要的对象拷贝,节省运算存储资源,提高效率。 +- 能够更简洁明确地定义泛型函数。 + +#### [引用折叠](https://www.huihut.com/interview/#/?id=%e5%bc%95%e7%94%a8%e6%8a%98%e5%8f%a0) + +- `X& &`、`X& &&`、`X&& &` 可折叠成 `X&` +- `X&& &&` 可折叠成 `X&&` + +C++的引用**在减少了程序员自由度的同时提升了内存操作的安全性和语义的优美性**。比如引用强制要求必须初始化,可以让我们在使用引用的时候不用再去判断引用是否为空,让代码更加简洁优美,避免了指针满天飞的情形。除了这种场景之外引用还用于如下两个场景: + +> 引用型参数 + +一般我们使用const reference参数作为只读形参,这种情况下既可以避免参数拷贝还可以获得与传值参数一样的调用方式。 + +```c++ +void test(const vector &data) +{ + //... +} +int main() +{ + vector data{1,2,3,4,5,6,7,8}; + test(data); +} +``` + +> 引用型返回值 + +C++提供了重载运算符的功能,我们在重载某些操作符的时候,使用引用型返回值可以获得跟该操作符原来语法相同的调用方式,保持了操作符语义的一致性。一个例子就是operator []操作符,这个操作符一般需要返回一个引用对象,才能正确的被修改。 + +```c++ +vector v(10); +v[5] = 10; //[]操作符返回引用,然后vector对应元素才能被修改 + //如果[]操作符不返回引用而是指针的话,赋值语句则需要这样写 +*v[5] = 10; //这种书写方式,完全不符合我们对[]调用的认知,容易产生误解 +``` + +## 3.指针与引用的性能差距 + +指针与引用之间有没有性能差距呢?这种问题就需要进入汇编层面去看一下。我们先写一个test1函数,参数传递使用指针: + +```c++ +void test1(int* p) +{ + *p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 + return; +} +``` + +该代码段对应的汇编代码如下: + +```c++ +(gdb) disassemble +Dump of assembler code for function test1(int*): + 0x0000000000400886 <+0>: push %rbp + 0x0000000000400887 <+1>: mov %rsp,%rbp + 0x000000000040088a <+4>: mov %rdi,-0x8(%rbp) +=> 0x000000000040088e <+8>: mov -0x8(%rbp),%rax + 0x0000000000400892 <+12>: movl $0x3,(%rax) + 0x0000000000400898 <+18>: nop + 0x0000000000400899 <+19>: pop %rbp + 0x000000000040089a <+20>: retq +End of assembler dump. + +``` + +上述代码1、2行是参数调用保存现场操作;第3行是参数传递,函数调用第一个参数一般放在rdi寄存器,此行代码把rdi寄存器值(指针p的值)写入栈中;第4行是把栈中p的值写入rax寄存器;第5行是把立即数3写入到**rax寄存器值所指向的内存**中,此处要注意(%rax)两边的括号,这个括号并并不是可有可无的,(%rax)和%rax完全是两种意义,(%rax)代表rax寄存器中值所代表地址部分的内存,即相当于C++代码中的*p,而%rax代表rax寄存器,相当于C++代码中的p值,所以汇编这里使用了(%rax)而不是%rax。 + +我们再写出参数传递使用引用的C++代码段test2: + +```c++ +void test2(int& r) +{ + r = 3; //赋值前无需判断reference是否为空 + return; +} +``` + +这段代码对应的汇编代码如下: + +```c++ +(gdb) disassemble +Dump of assembler code for function test2(int&): + 0x000000000040089b <+0>: push %rbp + 0x000000000040089c <+1>: mov %rsp,%rbp + 0x000000000040089f <+4>: mov %rdi,-0x8(%rbp) +=> 0x00000000004008a3 <+8>: mov -0x8(%rbp),%rax + 0x00000000004008a7 <+12>: movl $0x3,(%rax) + 0x00000000004008ad <+18>: nop + 0x00000000004008ae <+19>: pop %rbp + 0x00000000004008af <+20>: retq +End of assembler dump. + +``` + +我们发现test2对应的汇编代码和test1对应的汇编代码完全相同,这说明C++编译器在编译程序的时候将指针和引用编译成了完全一样的机器码。所以C++中的引用只是C++对指针操作的一个“语法糖”,在底层实现时C++编译器实现这两种操作的方法完全相同。 + +## 3.总结 + +C++中引入了引用操作,在对引用的使用加了更多限制条件的情况下,保证了引用使用的安全性和便捷性,还可以保持代码的优雅性。在适合的情况使用适合的操作,引用的使用可以一定程度避免“指针满天飞”的情况,对于提升程序鲁棒性也有一定的积极意义。最后,指针与引用底层实现都是一样的,不用担心两者的性能差距。 + +上述部分参考自: \ No newline at end of file diff --git a/pointer_refer/copy_construct b/pointer_refer/copy_construct new file mode 100755 index 0000000000000000000000000000000000000000..fa57fa35baa5c641a48b72ad46bc334f7f32c8af GIT binary patch literal 9592 zcmdT~eQZzjM#$J@?#?_wES~2V0t)4u@cHiaP~SV{`qIWj=*CdA%eRuTIPpez96y zC+2`til1Lf2%9a&Q_3~tGNtDNF2S$PX)@}Ze%WAroykTICUAs=slOTUtEP!N7MUbt zA-s}7b`Uls*!2G6-NcfgpXijT|uJ`sQ6MEVRr3_sc`rZ5WwrFoGZA4QI@lZM)O$)ub zW4j)SrWCtldqX^#h<1d!<55#qSaR=Gj*Nv5g!GUR zJrFZMkfusse<+p^TU(mz8}w>lO|Dh#+ibS??9n%YufU0YLc4)>Pzj#qKL_wlm@8V} zmG~`-#c;Bfh>+rCR>tPh4%?^Z(}A5tO>+kcVch&Qjw+{kK*^^TcR^~Nb&4fvUKe!c zs-si#%hh_B3yPv+@LKqMHj%8t!Y5x0l@>njX@)urpZZ|%Tlg4Y4m1mYevXUt)WTOs zs+4NOACzf_OFu313hl8mqjcgqu(jcr%g$Jg(VX7=E>x!1ox^kSYCl>;_mR@%_%vYM zpNOWEO}-)N-xE!fH902f-w{nInmi%t-x5u!nLIA(mx!j6OpZwUMWX2_pB$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- /dev/null +++ b/pointer_refer/copy_construct.cpp @@ -0,0 +1,35 @@ +/** + * @file copy_construct.cpp + * @brief g++ -o copy_construct copy_construct.cpp -fno-elide-constructors + * -fno-elide-constructors选项(关闭返回值优化) + * @author 光城 + * @version v1 + * @date 2019-08-09 + */ + +#include +using namespace std; + +class Copyable { +public: + Copyable(){} + Copyable(const Copyable &o) { + cout << "Copied" << endl; + } +}; +Copyable ReturnRvalue() { + return Copyable(); //返回一个临时对象 +} +void AcceptVal(Copyable a) { + +} +void AcceptRef(const Copyable& a) { + +} + +int main() { + cout << "pass by value: " << endl; + AcceptVal(ReturnRvalue()); // 应该调用两次拷贝构造函数 + cout << "pass by reference: " << endl; + AcceptRef(ReturnRvalue()); //应该只调用一次拷贝构造函数 +} diff --git a/pointer_refer/effec b/pointer_refer/effec new file mode 100755 index 0000000000000000000000000000000000000000..a7bf86a27a0754eb4badaaf586b0dc4f4fb8abb3 GIT binary patch literal 20576 zcmeHvdw5&bmG3^1Y|D1+SauQ|@<4%v2!b73u@fhRL{8$wrU6Xsgg^-=vMkvmvgGQK z2j!7AKnc$Pr40q9&|zk7TRPC&-VTorNp65ZDU@C=OiN3Lw$MpRpqUmJPoen!e)}QG zvfMABaNuV5U9jTHi;8qzia9G>%APtkjE1x7&uD)8LfLmq6z_+wv2AcA*Z2a|Vy}gbA zFya9Mu|_^~kh~1t@1*;sY=y#I6#*GBuvd|Vfky6P$kEP|4Kh*QPxnyrYz%g9d#)Nd-?A6l@0b-GyC_P|ggZl}`0WZJfceg1ZPLwF#XN{5rH zW5HA^oDz0d?*(>$IH}3K7p#sY;^E$4Uo5QBilAw!Bn`VD;`a+Y3|rA zY|s<2R7dyP4Lyk-y90!tbW1qiAL|PBq??;VgTbVoP6ngtR9C2@V*_Z5pdlKv6RC7E z931NEwR@ZFj-JJ^c6=VIRUS|oN@UX6+@b>I4w)jOw4rTwXmFby2}Wb^L1<^t4yMC9 zqmZ!4sU0#DjK;;f^n7bxmax!nTxpNO)3 zH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r&Vuy0oP@6OJ3=QEXX~!ChEgrm8}HACl`4Oul8 ztzFyGfzRLDSFP`mXM$L=W(~(7OMXdUWD-l)HJ4u4enHo2xsc~)_t8K=e$TGIT-RT_ zhf*2ou>57&$^2)4B0C*?e_u2UX%99^(YQ{=7xny(KL6wUf|T*v`5y!dwExwAPH>St zAdTCZ6po(c|1l{3zY6UBU^>WprORymhXYeD<|L#b)BH_Eo(tu=+qU~sI5*U~0Q3IU zK(2sjwkTfyKSC(T;{W=;A{6KevuNsMS~Oy>m=5*r~B#i36@?CNC-&RbGAgBXL@NK1xeK?>;DFuuEK zl-_DYv~eH-6#sq!Pj*`{C8X7j;X&O|2m7JKU`kXqd?jJxSBAibY=U^v0~-=)st1KG zXhgM$MjUz!VLSkh@8}>G#NjUJ3XR4i2@?;xz86*8N{5LPUV$I(f@JMC`?$C^*!bVrZ^~)lHpuhaPlKEN-aakR%}fwz zKY5J(q%m-vOjC`#*%!X0n@;CCggGLwAJ_fVT zd_@z?cf2Z9*=pj1d-2PcH~Y~OT7F+XGvYNGH}J~^^1I6dl4Ca;C4#Y6VdL=!kdU9h zS*P;$7ek+MtU|N-YgGI#BKh)WAN&FC3NdJ)5Il9W%HrQdjPltvR7F6g;bId*A-};5 z{4RJXZ_G6NYL}MprINg0H)RKD`IRi%u&JcLu$Dr~)B)QJVC)K_4l +using namespace std; +void test1(int* p) +{ + *p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 + return; +} + +void test2(int& p) +{ + p = 3; //此处应该首先判断p是否为空,为了测试的需要,此处我们没加。 + return; +} + +int main() { + int a=10; + int *p=&a; + test1(p); + test2(a); + cout<