該程式演示了list在記憶體分配時候的問題。裡面的備註資訊是我的想法。
#include #include #include #include using namespace std;
class cdata
cdata(int i, string &s)
cdata(const cdata &data)
cdata operator = (const cdata &data)
void setsequence(const int i)
void setremark(const string &s)
string tostring() const
; sprintf(tmp, "[sequence:%d | remark:%s]", this->sequence, this->remark.c_str());
//此處應該有記憶體複製操作,所以不用擔心返回後tmp陣列所指向的記憶體被修改或者不存在的情況。
return tmp;
} ~cdata()
protected:
private:
int sequence;
string remark;
};int main(int argc, char **argv)
cout << "***************clear list 1" << endl;
// list中的元素不是指標,此處的清理,會呼叫析構函式。
lst_data.clear();
cout << "********************==end of process" << endl;
return 0;
}
程式的結果:
process begin at 00b01749
cdata(int i,string &s) [sequence:1 | remark:baby_test] 010ffcf8
cdata(int i,string &s) [sequence:2 | remark:baby_test] 010ffcd0
cdata(int i,string &s) [sequence:3 | remark:baby_test] 010ffca8
cdata(int i,string &s) [sequence:4 | remark:baby_test] 010ffc80
cdata(int i,string &s) [sequence:5 | remark:baby_test] 010ffc58
cdata(int i,string &s) [sequence:6 | remark:baby_test] 010ffc30
cdata(int i,string &s) [sequence:7 | remark:baby_test] 010ffc08
cdata(int i,string &s) [sequence:8 | remark:baby_test] 010ffbe0
cdata(int i,string &s) [sequence:9 | remark:baby_test] 010ffbb8
cdata(int i,string &s) [sequence:10 | remark:baby_test] 010ffb90
push cdata to list
max size of listis 107374182
size of listis 0
****************lst_data.push_back(data1)
cdata(const cdata &data) [sequence:1 | remark:baby_test] 01176a58
****************lst_data.push_back(data2)
cdata(const cdata &data) [sequence:2 | remark:baby_test] 0117c7e8
****************lst_data.push_back(data3)
cdata(const cdata &data) [sequence:3 | remark:baby_test] 0117c840
****************lst_data.push_back(data4)
cdata(const cdata &data) [sequence:4 | remark:baby_test] 0117de90
****************lst_data.push_back(data5)
cdata(const cdata &data) [sequence:5 | remark:baby_test] 0117dee8
****************lst_data.push_back(data6)
cdata(const cdata &data) [sequence:6 | remark:baby_test] 0117df40
****************lst_data.push_back(data7)
cdata(const cdata &data) [sequence:7 | remark:baby_test] 0117e018
****************lst_data.push_back(data8)
cdata(const cdata &data) [sequence:8 | remark:baby_test] 0117e1d0
****************lst_data.push_back(data9)
cdata(const cdata &data) [sequence:9 | remark:baby_test] 0117e598
****************lst_data.push_back(data10)
cdata(const cdata &data) [sequence:10 | remark:baby_test] 0117e540
***************show list by iterator
address of itr is 01176a58 value is [sequence:1 | remark:baby_test]
address of itr is 0117c7e8 value is [sequence:2 | remark:baby_test]
address of itr is 0117c840 value is [sequence:3 | remark:baby_test]
address of itr is 0117de90 value is [sequence:4 | remark:baby_test]
address of itr is 0117dee8 value is [sequence:5 | remark:baby_test]
address of itr is 0117df40 value is [sequence:6 | remark:baby_test]
address of itr is 0117e018 value is [sequence:7 | remark:baby_test]
address of itr is 0117e1d0 value is [sequence:8 | remark:baby_test]
address of itr is 0117e598 value is [sequence:9 | remark:baby_test]
address of itr is 0117e540 value is [sequence:10 | remark:baby_test]
***************clear list 1
~cdata() 01176a58
~cdata() 0117c7e8
~cdata() 0117c840
~cdata() 0117de90
~cdata() 0117dee8
~cdata() 0117df40
~cdata() 0117e018
~cdata() 0117e1d0
~cdata() 0117e598
~cdata() 0117e540
********************==end of process
~cdata() 010ffb90
~cdata() 010ffbb8
~cdata() 010ffbe0
~cdata() 010ffc08
~cdata() 010ffc30
~cdata() 010ffc58
~cdata() 010ffc80
~cdata() 010ffca8
~cdata() 010ffcd0
~cdata() 010ffcf8
C C 記憶體分配機制
1.c語言中的記憶體機制 在c語言中,記憶體主要分為如下5個儲存區 1 棧 stack 位於函式內的區域性變數 包括函式實參 由編譯器負責分配釋放,函式結束,棧變數失效。2 堆 heap 由程式設計師用malloc calloc realloc分配,free釋放。如果程式設計師忘記free了,則會造...
memcached記憶體分配機制
memcached slab allocator分配機制 slab allocator的基本原理是按照預先規定的大小,將分配的記憶體分割成特定長度的塊,以完全解決記憶體碎片問題。slab allocation的原理相當簡單,就是將分配的記憶體分割成各種尺寸的塊 chunk 並把尺寸相同的塊分成組 c...
Memcache記憶體分配機制
1.page 頁 為記憶體分配的最小單位 memcached 的記憶體分配以page為單位,預設情況下乙個page是1m,可以通過 i引數在啟動時指定。如果需要申請記憶體時,memcached會劃分出乙個新的page並分配給需要的slab區域。page一旦被分配在重啟前不會被 或者重新分配 2.sl...