重载new和delete
1. 测试代码:
#include<iostream>
#include<new>
using namespace std;
class A {
public:
A() { cout << "A constructor" << endl; } void* operator new(size_t size)
{
cout << "this is A's new" << endl;
return ::operator new(size);
} void operator delete(void* ptr)
{
cout << "this is A's delete" << endl;
return ::operator delete(ptr);
} ~A() { cout << "A destructor" << endl; }
}; int main()
{
A *a = new A;
delete a;
return ;
}
运行结果:
虽然我们不能改变new/delete的行为,但是通过重载operator new() 和 operator delete()我们可以实现自己想要的内存管理方式,这在内存池的实现中十分关键。关于operator new有几点要注意:
(1)当无法满足所要求分配的空间时,则如果有new_handler,则调用new_handler,否则如果没要求不抛出异常(以nothrow参数表达),则执行bad_alloc异常,否则返回0
(2)重载时,返回类型必须声明为void*
(3)重载时,第一个参数类型必须为表达要求分配空间的大小(字节),类型为size_t
(4)重载时,可以带除(3)以外的其它参数
2.测试代码:
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std; void* operator new(size_t size)
{
cout << "global Override operator new: " << size << endl;
void * ptr = malloc(size);
return ptr;
} void* operator new(size_t size, int flag)
{
cout << "global Override operator new: " << size << " " << flag << endl;
return (::operator new(size));
} void operator delete (void* ptr)
{
cout << "global Override operator delete" << endl;
free(ptr);
ptr = nullptr;
} void operator delete (void* ptr, int flag)
{
cout << "Override operator delete: " << flag << endl;
::operator delete(ptr);
ptr = nullptr;
}
int main() {
int * ptr = new int();
delete ptr;
cout << endl << "*********************" << endl << endl;
ptr = new() int();
delete ptr;
return ;
}
输出结果:
定位new表达式
1. 测试代码:
#include <iostream>
using namespace std;
char addr1[];
int main()
{
cout << "******定位new表达式演示***by David***" << endl;
char addr2[];
char *addr3 = new char[];
cout << "addr1 = " << (void*)addr1 << endl;
cout << "addr2 = " << (void*)addr2 << endl;
cout << "addr3 = " << (void*)addr3 << endl;
int *p = nullptr; p = new(addr1)int; ////把内存分配到静态区
*p = ;
cout << (void*)p << " " << *p << endl; p = new(addr2)int; ////把内存分配到栈区
*p = ;
cout << (void*)p << " " << *p << endl; p = new(addr3)int; //把内存分配到堆区
*p = ;
cout << (void*)p << " " << *p << endl;
return ;
}
运行结果:
2.测试代码
#include <iostream>
#include <string>
#include <new> using namespace std;
const int BUF = ; class JustTesting {
private:
string words;
int number;
public:
JustTesting(const string &s = "Just Testing", int n = )
{
words = s;
number = n;
cout << words << " constructed\n";
} ~JustTesting() { cout << words << " destroyed\n"; }
void Show() const { cout << words << ", " << number << endl; }
}; int main(void)
{
char *buffer = new char[BUF]; // get a block of memory
JustTesting *pc1, *pc2; pc1 = new (buffer)JustTesting; // place object in buffer
pc2 = new JustTesting("heap1", ); // place object on heap cout << "Memory block address:\n" << "buffer: "
<< (void *)buffer << " heap: " << pc2 << endl;
cout << "Memory contents: \n";
cout << pc1 << ": ";
pc1->Show();
cout << pc2 << ": ";
pc2->Show(); JustTesting *pc3, *pc4;
pc3 = new (buffer) JustTesting("bad Idea", );
pc4 = new JustTesting("Heap2", ); cout << "Memory contents: \n";
cout << pc3 << ": ";
pc3->Show();
cout << pc4 << ": ";
pc4->Show(); delete pc2; // free heap1
delete pc4; // free heap2
delete[] buffer; // free buffer
cout << "Done\n"; return ;
}
运行结果: