我是 C 的新手,并試圖更好地理解記憶體是如何分配和存盤在類方法中的。
可以說我有以下代碼:
#include <iostream>
using namespace std;
class ExampleClass {
public:
void SetStoredAttribute(int Argument);
int GetStoredAttribute(void);
private:
int StoredAttribute;
};
void ExampleClass::SetStoredAttribute(int Argument) {
StoredAttribute = Argument;
};
int ExampleClass::GetStoredAttribute(void) {
return StoredAttribute;
};
int main() {
ExampleClass Object;
Object.SetStoredAttribute(1);
cout << Object.GetStoredAttribute();
return 0;
};
我的SetStoredAttribute方法完成后,C 對引數做了什么?它會釋放該記憶體還是我需要手動執行此操作?
uj5u.com熱心網友回復:
函式引數,包括成員函式中的引數,始終具有自動存盤持續時間。它們開始存在于函式作用域的開頭,而停止存在于函式作用域的結尾,并且實作處理確保它們被“分配”和“解除分配”。
在大多數平臺上,這是通過兩種方式實作的。
- 首先是堆疊,通常稱為堆疊,因為它是執行執行緒獨有的,CPU 中有一個專用暫存器指向頂部。
- 其次,引數直接分配給 CPU 中的暫存器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/450476.html
