第69課-技巧:自定義記憶體管理
阿新 • • 發佈:2018-12-12
第69課 技巧:自定義記憶體管理
一、筆試題
統計物件中某個成員變數的訪問次數
程式設計實驗:成員變數的訪問統計
實現方法一:使用mutable
#include<iostream>
using namespace std;
class Test{
int m_value;
mutable int m_count;
public:
Test(int value = 0){
m_value = value;
m_count = 0;
}
int getValue() const{
m_count++;
return m_value;
}
void setValue(int value){
m_count++;
m_value = value;
}
int getCount() const{
return m_count;
}
~Test(){
}
};
int main(){
Test t;
t.setValue(100 );
cout << "t.m_value = " << t.getValue() << endl;
cout << "t.m_count = " << t.getCount() << endl;
const Test ct(200);
cout << "ct.m_value = " << ct.getValue() << endl;
cout << "ct.m_count = " << ct.getCount() << endl;
return 0;
}
列印結果:
t.m_value = 100
t.m_count = 2
ct.m_value = 200
ct.m_count = 1
遺失的關鍵字:
mutable
是為了突破const
函式的限制而設計的mutable
成員變數將永遠處於可改變的狀態mutable
在實際的專案開發中被嚴禁濫用
mutable
的深入分析:
mutable
成員變數破壞了只讀物件的內部狀態const
成員函式保證只讀物件的狀態不變性mutable
成員變數的出現無法保證狀態不變性
實現方法二:使用指標
#include<iostream>
using namespace std;
class Test{
int m_value;
int * const m_pCount;
public:
Test(int value = 0) : m_pCount(new int(0)){
m_value = value;
}
int getValue() const{
*m_pCount += 1;
return m_value;
}
void setValue(int value){
*m_pCount += 1;
m_value = value;
}
int getCount() const{
return *m_pCount;
}
~Test(){
delete m_pCount; //注意不要忘了釋放指標
}
};
int main(int argc,char * argv[]){
Test t;
t.setValue(100);
cout << "t.m_value = " << t.getValue() << endl;
cout << "t.m_count = " << t.getCount() << endl;
const Test ct(200);
cout << "ct.m_value = " << ct.getValue() << endl;
cout << "ct.m_count = " << ct.getCount() << endl;
return 0;
}
列印結果相同。通過使用指標進行變數的修改
二、面試題二
new
關鍵字創建出來的物件位於什麼地方?
new/delete
的本質是C++預定義的操作符C++對這兩個操作符做了嚴格的行為定義
new
:-
- 獲取足夠大的記憶體空間(預設為堆空間)
- 在獲取的空間中呼叫建構函式建立物件
delete
-
- 呼叫解構函式銷燬物件
- 歸還物件所佔用的空間(預設為堆空間)
在C++中能夠過載new/delete
操作符
- 全域性過載(不推薦)
- 區域性過載(針對具體類進行過載)
過載new/delete
的意義在於改變動態物件建立時的記憶體分配方式
new/delete
的過載方式
//static member function
void* operator new (unsigned int size){
void* ret = NULL;
/* ret point to allocated memory */
return ret;
}
//static member function
void operator delete (void* p){
/* free the memory which is pointed by p */s
}
程式設計實驗:靜態儲存區中建立動態物件
#include<iostream>
using namespace std;
class Test{
static const unsigned int COUNT= 4;
static char c_buffer[];//將空間放置在靜態儲存區
static char c_map[];
int m_value;
public:
void* operator new (unsigned int size){//new過載
void* ret = NULL;
for(int i = 0;i < COUNT;++i){
if(!c_map[i]){
c_map[i] = 1;
ret = c_buffer + i * sizeof(Test);//指標加法
cout << "succeed to allocate memory:" << ret << endl;
break;
}
}
return ret;
}
void operator delete (void* p){ //delete過載
if(p != NULL){
char* mem = reinterpret_cast<char*>(p);
int index = (mem - c_buffer) / sizeof(Test);
int flag = (mem - c_buffer) % sizeof(Test);
if((flag == 0) && (0 <= index) && (index < COUNT)){
c_map[index] = 0;
cout << "succeed to free memory:" << p << endl;
}
}
}
};
char Test::c_buffer[sizeof(Test) * Test::COUNT] = {0};
char Test::c_map[Test::COUNT] = {0};
int main(int argc,char *argv[]){
cout << "========= Test Single Object =========" << endl;
Test* pt = new Test;
delete pt;
cout << "========= Test Object Array ==========" << endl;
Test* pa[5] = {0};
for(int i = 0;i < 5;++i){
pa[i] = new Test;
cout << "pa[" << i << "] = " << pa[i] << endl;
}
for(int i = 0;i < 5;++i){
cout << "delete " << pa[i] << endl;
delete pa[i];
}
return 0;
}
列印結果:
===== Test Single Object =====
succeed to allocate memory: 0x6021a0
succeed to free memory: 0x6021a0
===== Test Object Array =====
succeed to allocate memory: 0x6021a0
pa[0] = 0x6021a0
succeed to allocate memory: 0x6021a4
pa[1] = 0x6021a4
succeed to allocate memory: 0x6021a8
pa[2] = 0x6021a8
succeed to allocate memory: 0x6021ac
pa[3] = 0x6021ac
pa[4] = 0
delete 0x6021a0
succeed to free memory: 0x6021a0
delete 0x6021a4
succeed to free memory: 0x6021a4
delete 0x6021a8
succeed to free memory: 0x6021a8
delete 0x6021ac
succeed to free memory: 0x6021ac
delete 0
三、面試題三
如何在指定的地址上建立C++物件?
解決方案:
- 在類中過載
new/delete
操作符 - 在
new
的操作符過載函式中返回指定的地址 - 在
delete
操作符過載中標記對應的地址可用
程式設計實驗:自定義動態物件的儲存空間
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Test
{
static unsigned int c_count;
static char* c_buffer;
static char* c_map;
int m_value;
public:
static bool SetMemorySource(char* memory, unsigned int size)
{
bool ret = false;
c_count = size / sizeof(Test);
ret = (c_count && (c_map = reinterpret_cast<char*>(calloc(c_count, sizeof(char)))));
if( ret )
{
c_buffer = memory;
}
else
{
free(c_map);
c_map = NULL;
c_buffer = NULL;
c_count = 0;
}
return ret;
}
void* operator new (unsigned int size)
{
void* ret = NULL;
if( c_count > 0 )
{
for(int i=0; i<c_count; i++)
{
if( !c_map[i] )
{
c_map[i] = 1;
ret = c_buffer + i * sizeof(Test);
cout << "succeed to allocate memory: " << ret << endl;
break;
}
}
}
else
{
ret = malloc(size);
}
return ret;
}
void operator delete (void* p)
{
if( p != NULL )
{
if( c_count > 0 )
{
char* mem = reinterpret_cast<char*>(p);
int index = (mem - c_buffer) / sizeof(Test);
int flag = (mem - c_buffer) % sizeof(Test);
if( (flag == 0) && (0 <= index) && (index < c_count) )
{
c_map[index] = 0;
cout << "succeed to free memory: " << p << endl;
}
}
else
{
free(p);
}
}
}
};
unsigned int Test::c_count = 0;
char* Test::c_buffer = NULL;
char* Test::c_map = NULL;
int main(int argc, char *argv[])
{
char buffer[12] = {0};
Test::SetMemorySource(buffer, sizeof(buffer));
cout << "===== Test Single Object =====" << endl;
Test* pt = new Test;
delete pt;
cout << "===== Test Object Array =====" << endl;
Test* pa[5] = {0};
for(int i=0; i<5; i++)
{
pa[i] = new Test;
cout << "pa[" << i << "] = " << pa[i] << endl;
}
for(int i=0; i<5; i++)
{
cout << "delete " << pa[i] << endl;
delete pa[i];
}
return 0;
}
列印結果
===== Test Single Object =====
succeed to allocate memory: 0x7ffff9dabb90
succeed to free memory: 0x7ffff9dabb90
===== Test Object Array =====
succeed to allocate memory: 0x7ffff9dabb90
pa[0] = 0x7ffff9dabb90
succeed to allocate memory: 0x7ffff9dabb94
pa[1] = 0x7ffff9dabb94
succeed to allocate memory: 0x7ffff9dabb98
pa[2] = 0x7ffff9dabb98
pa[3] = 0
pa[4] = 0
delete 0x7ffff9dabb90
succeed to free memory: 0x7ffff9dabb90
delete 0x7ffff9dabb94
succeed to free memory: 0x7ffff9dabb94
delete 0x7ffff9dabb98
succeed to free memory: 0x7ffff9dabb98
delete 0
delete 0
四、被忽略的事實
new[] / delete[]
與 new /delete
完全不同
- 動態物件陣列建立通過new[] 完成
- 動態物件陣列的銷燬通過delete[]完成
- new[] / delete[] 能夠被過載,進而改變記憶體管理方式
new[] / delete[] 的過載方式
//static member function
void* operator new[] (unsigned int size){
return malloc(size);
}
//static member function
void operator delete[] (void* p){
free(p);
}
注意事項:
- new[]實際需要返回的記憶體空間可能比期望的要多
- 物件陣列佔用的記憶體中需要儲存陣列資訊 (比期望多的部分)
- 陣列資訊用於確定建構函式和解構函式的呼叫次數
程式設計實驗:動態陣列的記憶體管理
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Test
{
int m_value;
public:
Test()
{
m_value = 0;
}
~Test()
{
}
void* operator new (unsigned int size)
{
cout << "operator new: " << size << endl;
return malloc(size);
}
void operator delete (void* p)
{
cout << "operator delete: " << p << endl;
free(p);
}
void* operator new[] (unsigned int size)
{
cout << "operator new[]: " << size << endl;
return malloc(size);
}
void operator delete[] (void* p)
{
cout << "operator delete[]: " << p << endl;
free(p);
}
};
int main(int argc, char *argv[])
{
Test* pt = NULL;
//都是成對出現的,不能交錯,因為二者使用的地址可能不相同
pt = new Test;
delete pt;
pt = new Test[5];
delete[] pt;
return 0;
}
列印結果:
operator new: 4
operator delete: 0x2111e30
operator new[]: 28
operator delete[]: 0x2111e50
五、總結
new/delete
的本質為操作符- 可以通過全域性函式過載
new /delete
(不推薦) - 可以針對具體的類過載
new /delete
new[] / delete[]
與new /delete
完全不同new[] / delete[]
也是可以被過載的操作符new[]
返回的記憶體空間可能比期望的要多