網(wǎng)站首頁 編程語言 正文
前言
很久以前閱讀了 CSAPP 這本書,可惜看過的東西基本都忘記了,只知道一些工具可以幫助我分析。今天突然對 “返回對象的函數(shù)” 很感興趣,于是分析了一下匯編。
返回對象的函數(shù)如下,現(xiàn)在有一種叫做 NRV 優(yōu)化的技術(shù)可以避免低效率,如果把它關(guān)掉,它將會執(zhí)行以下過程:創(chuàng)建一個對象 apple,然后返回一個 apple,發(fā)生一次拷貝構(gòu)造函數(shù),所以存在執(zhí)行效率問題;如果你還寫了 Apple a = GetApple();
,那么還將會發(fā)生一次賦值拷貝構(gòu)造。
Apple GetApple() { Apple apple{}; return apple; }
代碼:
class Apple { public: Apple() { } ~Apple() { } Apple(const Apple& apple) { this->a = apple.a; this->b = apple.b; } Apple& operator=(const Apple& apple) { this->a = apple.a; this->b = apple.b; return *this; } void Print() { cout << a << " " << b << endl; } int a = 1; int b = 2; }; Apple GetApple() { Apple apple{}; return apple; }
構(gòu)造函數(shù)的執(zhí)行過程分析
構(gòu)造函數(shù)的執(zhí)行過程:即使是無參構(gòu)造函數(shù),調(diào)用之前仍然要傳參。傳入的是一個地址,需要在函數(shù)運行結(jié)束的時候,這個地址上有了一個對象。
main 函數(shù)如下:
int main() { Apple a; int x = a.a; x = 10; return 0; }
生成的匯編代碼:
# main 函數(shù)構(gòu)造對象的地方: pushq %rbp # rbp 壓棧 movq %rsp, %rbp # rsp 替換 rbp pushq %rbx # rbx 壓棧,保存現(xiàn)場 subq $24, %rsp # rsp 自減 24,棧向下增長,相當(dāng)于擴(kuò)展 24 字節(jié) leaq -24(%rbp), %rax # 將地址賦值給 rax,rax 是 Apple 對象開始的地方 movq %rax, %rdi # rdi 傳參 call _ZN5AppleC1Ev movl -28(%rbp), %eax # 返回之后,會執(zhí)行 int x = a.a; movl %eax, -20(%rbp) # 取第一個字節(jié)賦值給 x movl $10, -20(%rbp) # 直接使用 10 賦值給 x;寫著行的目的只是為了確定 x 的位置 # 默認(rèn)構(gòu)造器: pushq %rbp # rbp 壓棧 movq %rsp, %rbp # rsp 替換 rbp movq %rdi, -8(%rbp) # rdi 是前面 rax 的值 movq -8(%rbp), %rax # 設(shè)置 rax 為 rbp 減 8 movl $1, (%rax) # 間接尋址,設(shè)置 4 字節(jié)為 1 movq -8(%rbp), %rax # rax 其實還是一樣的 movl $2, 4(%rax) # 變址尋址,其實就是間接尋址加一個偏移量,設(shè)置 4 字節(jié)為 2 nop popq %rbp ret # 于是最終 rdi 開始,向下的 8 字節(jié)是 Apple 對象
返回對象函數(shù)的分析
從匯編的視角來看,調(diào)用構(gòu)造器和調(diào)用 “返回對象” 的函數(shù)是一樣的。它的執(zhí)行過程是:即使是兩個函數(shù)都是無參的,它仍然會進(jìn)行一次傳參。傳入的是一個地址,需要在函數(shù)調(diào)用結(jié)束后,這個地址上有了一個對象。從匯編的角度來看,對象就是一堆數(shù)據(jù)的排列,比如說最普通的對象就是數(shù)據(jù)成員按照聲明順序直接排列。
舉個實際點的例子。Apple 這個類,有兩個成員,a 和 b。調(diào)用了構(gòu)造器或者 “返回對象” 的函數(shù),先傳入一個地址,之后函數(shù)里面會在這個地址上存放兩個數(shù),分別是 a 和 b 的值,然后返回。此時,這個地址上就有了 a 和 b,雖然機(jī)器看不到對象,但是對我們來說,它就是一個對象。
如果關(guān)閉了 NRV 優(yōu)化,那么首先會傳入一個地址,然后構(gòu)造一個對象,這個對象將會被拷貝構(gòu)造到傳入的地址上,返回。之后如果調(diào)用了一次賦值,那么還要將這個地址上的對象復(fù)制構(gòu)造給棧上的變量。整個過程,實際上存在兩個臨時對象,發(fā)生了一次構(gòu)造、一次復(fù)制構(gòu)造、一次賦值構(gòu)造。
main: .LFB3535: pushq %rbp movq %rsp, %rbp pushq %rbx subq $40, %rsp leaq -36(%rbp), %rax movq %rax, %rdi call _ZN5AppleC1Ev movl -36(%rbp), %eax movl %eax, -20(%rbp) movl $10, -20(%rbp) leaq -28(%rbp), %rax # 這里之前的匯編是一樣的,調(diào)用 GetApple movq %rax, %rdi # rdi,傳參 call _Z8GetApplev leaq -28(%rbp), %rdx # rax 已經(jīng)存放對象,-28 的位置有對象 leaq -36(%rbp), %rax movq %rdx, %rsi movq %rax, %rdi call _ZN5AppleaSERKS_ leaq -28(%rbp), %rax movq %rax, %rdi call _ZN5AppleD1Ev movl $0, %ebx leaq -36(%rbp), %rax movq %rax, %rdi call _ZN5AppleD1Ev movl %ebx, %eax addq $40, %rsp popq %rbx popq %rbp # GetApple 函數(shù)的匯編代碼 pushq %rbp movq %rsp, %rbp subq $32, %rsp movq %rdi, -24(%rbp) # 分配空間,然后將傳過來的 rdi 放進(jìn)去 leaq -8(%rbp), %rax movq %rax, %rdi # 前面分析過了,調(diào)用回來時 rax 開始的 8 字節(jié)就是對象 call _ZN5AppleC1Ev leaq -8(%rbp), %rdx # 取 rax 地址到 rdx movq -24(%rbp), %rax # 取 rdi 地址到 rax movq %rdx, %rsi # rsi 已經(jīng)有對象了 movq %rax, %rdi # rsi 和 rdi 都用來傳參 call _ZN5AppleC1ERKS_ # 調(diào)用拷貝構(gòu)造函數(shù) leaq -8(%rbp), %rax # rax 上有對象了 movq %rax, %rdi # rdi 傳參,準(zhǔn)備調(diào)用析構(gòu)函數(shù) call _ZN5AppleD1Ev nop movq -24(%rbp), %rax # 前面調(diào)用拷貝構(gòu)造前的地址,這個地址有對象了 leave ret # 拷貝構(gòu)造函數(shù) pushq %rbp movq %rsp, %rbp movq %rdi, -8(%rbp) # rdi 無對象 movq %rsi, -16(%rbp) # rsi 有對象 movq -16(%rbp), %rax # 把對象放到 rax movl (%rax), %edx # 把第一屬性(4 字節(jié))移動到 edx movq -8(%rbp), %rax movl %edx, (%rax) # 把第一屬性移動到 rdi movq -16(%rbp), %rax movl 4(%rax), %edx # 把第二屬性移動到 edx movq -8(%rbp), %rax movl %edx, 4(%rax) # 把第二屬性移動到 rdi 上 movq -8(%rbp), %rax # rdi 上有了對象,rax 也設(shè)置一個 popq %rbp ret
原文鏈接:https://www.cnblogs.com/zzk0/p/15827536.html
相關(guān)推薦
- 2022-07-09 flutter封裝單選點擊菜單工具欄組件_Android
- 2022-12-26 層次分析法在matlab上的實現(xiàn)方式_python
- 2022-03-19 MongoDB數(shù)據(jù)庫授權(quán)認(rèn)證的實現(xiàn)_MongoDB
- 2022-05-13 Linux操作系統(tǒng)筆記——GCC編譯器
- 2022-07-24 .Net結(jié)構(gòu)型設(shè)計模式之享元模式(Flyweight)_基礎(chǔ)應(yīng)用
- 2022-03-14 npm 更改為淘寶鏡像的方法
- 2022-02-16 mac 使用launchctl 開機(jī)時加速vim、emacs
- 2022-03-03 GitHub 私人private倉庫添加成員(協(xié)作者Collaborators)
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支