* omnia neo:
> Hello,
>
> I am trying to redirect the standard malloc and calloc defined in
> stdlib.h to myown implementation MyMalloc amd MyCalloc. Following is
> my impementation:
>
> \\MyMemory.c
> #include MyMemory.h /*implementation of MyMalloc() and MyCalloc()*/
> #define malloc MyMalloc
>
> But when i compile my code in VisualStudios I get following error:
> ..\VC\include\stdlib.h(601) : error C2375: 'MyMalloc' : redefinition;
> different linkage
> ..\MyMemory.h(41) : see declaration of 'MyMalloc'
>
> Please help me know how do I redirect the standard calls ?
In C++ 'malloc' is seldom used directly.
It *may* be used indirectly by C++ allocation, but that's an implementation detail.
To redirect C++ allocation you overload or define the allocation and
deallocation functions, 'operator new', 'operator new[]', 'operator delete' and
'operator delete[]'.
You can overload the global ones, and/or define/overload per class.
Make sure that you never return nullpointers but instead throw std::bad_alloc.
Cheers & hth.,
- Alf
|