1

I'm not that advanced in C++ so I hope it's an adequate question.

My situation is that I have a large legacy C code-base system and a small C++ application which I want to integrate it with.

One of the requirements is that this legacy system has its own memory allocation/deallocation methods and they must be used. In other words, since using C++ with, for example, std libraries typically calls delete() for memory deallocation, I am interested in replacing that with my legacy system's deallocation functionality which we can call tfree() (and allocation done through talloc()).

I believe that there must be some way I can inject my own deallocation method in place of delete() for the entirety of the C++ application, but I don't know how or if it is at all possible. I would appreciate any help

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Zeruno
  • 1,391
  • 2
  • 20
  • 39
  • 2
    Yes it is possible, the standard library uses [`allocators`](https://en.cppreference.com/w/cpp/named_req/Allocator) to allocate and deallocate memory. You can write your own and provide it as a template parameter to types like `std::vector`, `std::string` and so on. – Timo Jul 07 '19 at 17:43
  • 4
    You can also provide your own `operator new` and `operator delete` globally: http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml https://stackoverflow.com/questions/639676/overloading-global-operator-new-delete-in-c I cannot find any modern C++11/14/17 resources on this topic though. – R2RT Jul 07 '19 at 17:44
  • 1
    Famous Meyers 'Effective C++' has a chapter 8 'Customizing new and delete', where this topic widely covered. It may be helpful. – gimme_danger Jul 07 '19 at 17:55
  • 1
    Interesting, `operator new` and `operator delete` have [special behavior](https://en.cppreference.com/w/cpp/memory/new/operator_new#Global_replacements) when it comes to visibility of the definition. – Timo Jul 07 '19 at 17:56
  • 1
    @Timo I always believed they are implemented (can be considered) as [weak symbols](https://en.wikipedia.org/wiki/Weak_symbol) for the linker – R2RT Jul 07 '19 at 18:17

0 Answers0