当前位置:   article > 正文

C++ 操作符重载new delete_赋值运算符重载为什么要delete

赋值运算符重载为什么要delete

 在C++中,操作符重载让C++拥有很大的灵活性,除了可以重载加减乘除等操作符,new、delete也可以被用户重载。

为什么要重载new、delete?

 重载new、delete主要有以下两个用途:
1.不把内存分配到堆上(new通常将内存分配到堆中),或者减少碎片内存。
2.可以做一些记录,例如跟踪内存的实际使用情况,用于检查内存是否存在泄露。

如何重载new、delete?

new操作符重载

 C++中给我们提供了4中new操作符重载:

    void* operator new  (std::size_t count );  
    void* operator new[](std::size_t count );  
    void* operator new  (std::size_t count, const std::nothrow_t& tag);  
    void* operator new[](std::size_t count, const std::nothrow_t& tag);  
  • 1
  • 2
  • 3
  • 4

 不难发现每个函数有些共同点:

  1. 都带有一个size_t 类型的参数,这将指定分配的内存的大小。
  2. 返回值都必须为void* 类型,用于返回分配内存的首地址。

 如果不能正确分配内存,那么1 2两种将会触发一个std::bad_alloc异常,而3 4则会返回一个空指针NULL。

  • 正常情况下,1 2足以满足使用
  • 2 4专门用于处理数组的内存分配

但是要注意的是:new的重载只是改变的内存分配的方式,并无法改变对象的构造,同理下面的delete也只是改变了内存释放的方式。

delete操作符重载

以及5中delete操作符重载:

	void operator delete  (void* ptr); 	
	void operator delete[](void* ptr);
	void operator delete  (void* ptr, const std::nothrow_t& tag);
	void operator delete[](void* ptr, const std::nothrow_t& tag);
	void operator delete  (void* ptr, std::size_t sz);
	void operator delete[](void* ptr, std::size_t sz);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

delete都将会接受一个void*类型的参数,即将要释放内存的首地址。

代码实现

 在上面重载的原因中我们提到,重载new、delete可以帮助我们观察我们内存的使用情况,下面我们将来实现这一功能。


	// myNew.hpp
	
	#ifndef MY_NEW
	#define MY_NEW
	
	#include <cstdlib>
	#include <iostream>
	#include <new>
	
	static std::size_t alloc{0};
	static std::size_t dealloc{0};
	
	void* operator new(std::size_t sz)
	{
	    alloc+= 1;//将可以观测内存分配的情况
	    return std::malloc(sz);
	}
	
	void operator delete(void* ptr) noexcept
	{
	    dealloc+= 1;//将可以观测内存释放的情况
	    std::free(ptr);
	}
	//打印内存的实际使用情况
	void getInfo()
	{
	    
	    std::cout << std::endl;
	 
	    std::cout << "Number of allocations: " << alloc << std::endl;
	    std::cout << "Number of deallocations: " << dealloc << std::endl;
	    
	    std::cout << std::endl;
	}
	
	#endif // MY_NEW
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

 我们当然也可以选择将分配的内存放入到一个数组中,通过遍历这个数组来检测内存是否泄露


	// myNew3.hpp
	
	#ifndef MY_NEW3
	#define MY_NEW3
	
	#include <algorithm>
	#include <cstdlib>
	#include <iostream>
	#include <new>
	#include <string>
	#include <array>
	
	int const MY_SIZE= 10;
	
	std::array<void* ,MY_SIZE> myAlloc{nullptr,};//存储内存地址以及大小的数组
	    
	void* operator new(std::size_t sz)
	{
	    static int counter{};
	    void* ptr= std::malloc(sz);
	    myAlloc.at(counter++)= ptr;//向数组中添加元素
	    std::cerr << "Addr.: " << ptr << " size: " << sz << std::endl;
	    return ptr;
	}
	
	void operator delete(void* ptr) noexcept
	{
	    auto ind= std::distance(myAlloc.begin(),std::find(myAlloc.begin(),myAlloc.end(),ptr));//找到目标内存的下标
	    myAlloc[ind]= nullptr;
	    std::free(ptr);
	}
	
	void getInfo()
	{
	    
	    std::cout << std::endl;
	     
	    std::cout << "Not deallocated: " << std::endl;
	    for (auto i: myAlloc)
		{
	        if (i != nullptr ) std::cout << " " << i << std::endl;//打印没有被释放的内存
	    }
	    
	    std::cout << std::endl;
	    
	}
	
	#endif // MY_NEW3
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/876145
推荐阅读
相关标签
  

闽ICP备14008679号