M
MJ_India
In my current requirement I want to manage memory of some modules from
a pool.
All the memory allocations (standard and user defined data types) in
these modules must fetch memory from some pool. A memory allocated in
1 module is always freed in same module and is never freed in any
other module.
But I don't know how to override new / delete operator.
1) If I override the definition globally, it overrides the allocation
and deallocation in modules where I want heap memory.
2) If I add extra parameters to new and delete [for ex: void *operator
new(size_t count, bool dummy), void delete(void *where, bool dummy)]
and redefine new to new(true) in required modules.
a) Operator new (and new[]) works fine, but I don't know how to make
the delete work.
b) I don't know if it is standard and would be supported by all C++
compilers.
3) It works fine If I override new/delete in some modules and make
them static. (I tried on Microsoft Visual C++ environment)
But again I don't know if it legal to make new and delete static. (As
they can be either global or defined for a class)
4) I tried to redefine new/delete in a namespace and use that
namespace where I want new definition. But Compiler either ignores it
(when I use "using namespace memory_mngr") or prompt a warning telling
new/delete must be global (When I used memory_mngr::new).
Please help me to find the standard solution for this problem. I want
to use new and delete (and new[], delete[]) in their natural syntax
for all my allocations and deallocations. Thank you in advance.
a pool.
All the memory allocations (standard and user defined data types) in
these modules must fetch memory from some pool. A memory allocated in
1 module is always freed in same module and is never freed in any
other module.
But I don't know how to override new / delete operator.
1) If I override the definition globally, it overrides the allocation
and deallocation in modules where I want heap memory.
2) If I add extra parameters to new and delete [for ex: void *operator
new(size_t count, bool dummy), void delete(void *where, bool dummy)]
and redefine new to new(true) in required modules.
a) Operator new (and new[]) works fine, but I don't know how to make
the delete work.
b) I don't know if it is standard and would be supported by all C++
compilers.
3) It works fine If I override new/delete in some modules and make
them static. (I tried on Microsoft Visual C++ environment)
But again I don't know if it legal to make new and delete static. (As
they can be either global or defined for a class)
4) I tried to redefine new/delete in a namespace and use that
namespace where I want new definition. But Compiler either ignores it
(when I use "using namespace memory_mngr") or prompt a warning telling
new/delete must be global (When I used memory_mngr::new).
Please help me to find the standard solution for this problem. I want
to use new and delete (and new[], delete[]) in their natural syntax
for all my allocations and deallocations. Thank you in advance.