Topic: Templated malloc, calloc and realloc - remove all the 'new's!


Author: robbepop@web.de
Date: Sun, 2 Aug 2015 15:15:10 -0700 (PDT)
Raw View
------=_Part_434_270275371.1438553710290
Content-Type: multipart/alternative;
 boundary="----=_Part_435_417757010.1438553710290"

------=_Part_435_417757010.1438553710290
Content-Type: text/plain; charset=UTF-8

Since recent standards banned operator new's usage for modern C++ programs,
wouldn't make it sense to take initial steps in order to make the language
even less dependent on this operator?

The only places where one actually uses 'new' in modern C++ is, when
writing an allocater and even there one could just use plain old C malloc.
We could lay more focus on malloc and create some useful templates for it
in order to make C++ devs more happy when encountering those in the wild.

What about template functions for:

T* mem_alloc<T>(size_t size = 1u) noexcept;
T* mem_calloc<T>(size_t size) noexcept;
T* mem_realloc<T>(size_t size) noexcept;

(or malloc<T>, calloc<T>, realloc<T> ...)

Internally those could easily use reinterpret_cast<T*> to convert the void*
of the C API.

Usage example:
auto p = malloc<T>();
instead of:
auto p = reinterpret_cast<T*>(malloc(sizeof(T)));
=> implicit 1 element - no ugly casts!

auto arr = malloc<T>(n);
instead of:
auto arr = reinterpret_cast<T*>(malloc(n * sizeof(T)));
=> allocates an array of size n on heap

auto q = calloc<T>(n);
instead of:
auto q = reinterpret_cast<T*>(malloc(n, sizeof(T)));
=> allocates n elements of type T initialized by 0. (this function could be
improved to default initialize or initialize by a given value)

auto q = realloc<T>(p, n); // where 'p' is type of T*
instead of:
auto q = reinterpret_cast<T*>(realloc(p, n * sizeof(T)));

These changes would enhance usage of modern malloc and reduce usage of the
otherwise useless 'new' operator which could lead to a long term
deprecation of this operator.

Regards,
Rob

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_435_417757010.1438553710290
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Since recent standards banned operator new&#39;s usage for=
 modern C++ programs, wouldn&#39;t make it sense to take initial steps in o=
rder to make the language even less dependent on this operator?<br><br>The =
only places where one actually uses &#39;new&#39; in modern C++ is, when wr=
iting an allocater and even there one could just use plain old C malloc.<br=
>We could lay more focus on malloc and create some useful templates for it =
in order to make C++ devs more happy when encountering those in the wild.<b=
r><br>What about template functions for:<br><br>T* mem_alloc&lt;T&gt;(size_=
t size =3D 1u) noexcept;<br>T* mem_calloc&lt;T&gt;(size_t size) noexcept;<b=
r>T* mem_realloc&lt;T&gt;(size_t size) noexcept;<br><br>(or malloc&lt;T&gt;=
, calloc&lt;T&gt;, realloc&lt;T&gt; ...)<br><br>Internally those could easi=
ly use reinterpret_cast&lt;T*&gt; to convert the void* of the C API.<br><br=
>Usage example:<br>auto p =3D malloc&lt;T&gt;();<br>instead of:<br>auto p =
=3D reinterpret_cast&lt;T*&gt;(malloc(sizeof(T)));<br>=3D&gt; implicit 1 el=
ement - no ugly casts!<br><br>auto arr =3D malloc&lt;T&gt;(n);<br>instead o=
f:<br>auto arr =3D reinterpret_cast&lt;T*&gt;(malloc(n * sizeof(T)));<br>=
=3D&gt; allocates an array of size n on heap<br><br>auto q =3D calloc&lt;T&=
gt;(n);<br>instead of:<br>auto q =3D reinterpret_cast&lt;T*&gt;(malloc(n, s=
izeof(T)));<br>=3D&gt; allocates n elements of type T initialized by 0. (th=
is function could be improved to default initialize or initialize by a give=
n value)<br><br>auto q =3D realloc&lt;T&gt;(p, n); // where &#39;p&#39; is =
type of T*<br>instead of:<br>auto q =3D reinterpret_cast&lt;T*&gt;(realloc(=
p, n * sizeof(T)));<br><br>These changes would enhance usage of modern mall=
oc and reduce usage of the otherwise useless &#39;new&#39; operator which c=
ould lead to a long term deprecation of this operator.<br><br>Regards,<br>R=
ob<br></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_435_417757010.1438553710290--
------=_Part_434_270275371.1438553710290--

.