Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is it possible to reimplement new and delete?

38 views
Skip to first unread message

wyn...@gmail.com

unread,
May 10, 2018, 11:02:38 AM5/10/18
to
Recently I had an idea to reimplement new as a function or template
using C malloc. An application example is to implement 'new *this'.
I don't use std-library, most of my codes are 'home-made', only few are
using std:: things (std::terminate(), std::numeric_limits, <typeinfo> )
So is it doable? Any sugestion will be appreciated.

Alf P. Steinbach

unread,
May 10, 2018, 11:12:18 AM5/10/18
to
On 10.05.2018 17:02, wyn...@gmail.com wrote:
> Recently I had an idea to reimplement new as a function or template
> using C malloc.

Why would you want to do use malloc for C++ `new`?


> An application example is to implement 'new *this'.

That's not valid C++ syntax.

So, it's not really an example of anything.


> I don't use std-library, most of my codes are 'home-made', only few are
> using std:: things (std::terminate(), std::numeric_limits, <typeinfo> )
> So is it doable?

Yes, you can override the allocation and deallocation functions,
operator new, operator new[], operator delete and operator delete[], in
the global namespace as well as per class.


> Any sugestion will be appreciated.

I would suggest not doing this. ;-)


Cheers!,

- Alf

Bo Persson

unread,
May 10, 2018, 11:49:51 AM5/10/18
to
Before putting too much work into this, you might want to check what
your compiler does by default.

For example, the compiler I'm using does this:


void* __CRTDECL operator new(size_t const size)
{
for (;;)
{
if (void* const block = malloc(size))
{
return block;
}

if (_callnewh(size) == 0)
{
if (size == SIZE_MAX)
{
__scrt_throw_std_bad_array_new_length();
}
else
{
__scrt_throw_std_bad_alloc();
}
}

// The new handler was successful; try to allocate again...
}
}




Bo Persson


wyn...@gmail.com

unread,
May 11, 2018, 9:44:12 AM5/11/18
to
Alf P. Steinbach於 2018年5月10日星期四 UTC+8下午11時12分18秒寫道:
> On 10.05.2018 17:02, wyn...@gmail.com wrote:
> > Recently I had an idea to reimplement new as a function or template
> > using C malloc.
>
> Why would you want to do use malloc for C++ `new`?
>
>
> > An application example is to implement 'new *this'.
>
> That's not valid C++ syntax.
>
> So, it's not really an example of anything.
>

I think the related 'clone()' problem had been an topic long long ago.
Probably it's just a quick though flashed in my brain.

>
> > I don't use std-library, most of my codes are 'home-made', only few are
> > using std:: things (std::terminate(), std::numeric_limits, <typeinfo> )
> > So is it doable?
>
> Yes, you can override the allocation and deallocation functions,
> operator new, operator new[], operator delete and operator delete[], in
> the global namespace as well as per class.
>
>
> > Any sugestion will be appreciated.
>
> I would suggest not doing this. ;-)
>
>
> Cheers!,
>
> - Alf

pretty encouraging!

wyn...@gmail.com

unread,
May 11, 2018, 9:56:47 AM5/11/18
to
Bo Persson於 2018年5月10日星期四 UTC+8下午11時49分51秒寫道:
Thanks for the example, helpful to know C's malloc can implement new.

Probable I had used a wrong word (reimplement new).

Paavo Helde

unread,
May 11, 2018, 11:42:32 AM5/11/18
to
On 11.05.2018 16:44, wyn...@gmail.com wrote:
> Alf P. Steinbach於 2018年5月10日星期四 UTC+8下午11時12分18秒寫道:
>> On 10.05.2018 17:02, wyn...@gmail.com wrote:
>>> Recently I had an idea to reimplement new as a function or template
>>> using C malloc.
>>
>> Why would you want to do use malloc for C++ `new`?
>>
>>
>>> An application example is to implement 'new *this'.
>>
>> That's not valid C++ syntax.
>>
>> So, it's not really an example of anything.
>>
>
> I think the related 'clone()' problem had been an topic long long ago.
> Probably it's just a quick though flashed in my brain.

The cloning issue should become nicely resolved by the metaclasses
proposal, but I'm told this is still far away.

Anyway, I don't see how malloc would help in any way with cloning.

wyn...@gmail.com

unread,
May 12, 2018, 11:16:22 AM5/12/18
to
Paavo Helde於 2018年5月11日星期五 UTC+8下午11時42分32秒寫道:
Good to know this information, many thanks.

So far, I had only *one* case using the clone function, and the need
of that usecase was not strong enough, neither. I might be also
thinking something else about using malloc for operator new, but not
in a hurry. Probably just preparing to face the future C++.
0 new messages