Page 1 of 1

Operators new and delete

Posted: Tue Jun 09, 2020 10:50 am
by 8infy
Hi guys, just implemented my first kernel heap allocator and I'm kinda confused about the C++ operators...

Why does this call the delete with size_t?
Image

I did read this page https://en.cppreference.com/w/cpp/memor ... tor_delete but it says that they're called
only if user defined implementation is provided, but if I don't provide them GCC starts complaining and it results in an undefined reference...
(I'm using C++17)

Thanks :)

Re: Operators new and delete

Posted: Tue Jun 09, 2020 11:46 am
by nexos
I am not a C++ expert, I mainly know C, but in the picture, it says there are 4 overloads. That tells me that is one way to define it. You could also try using a lower standard and see if that helps.

Re: Operators new and delete

Posted: Tue Jun 09, 2020 6:06 pm
by kzinti
It is rather trivial to implement new/delete. Here is what I use (I didn't implement all overloads as I didn't need them):

Code: Select all

inline void* operator new(size_t, void* p)      { return p; }
inline void* operator new[](size_t, void* p)    { return p; }
inline void* operator new(size_t size)          { return ::malloc(size); }

inline void operator delete(void* p)            { ::free(p); }
inline void operator delete(void* p, size_t)    { ::free(p); }