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

HOW can I use custom placement new with array?

35 views
Skip to first unread message

Christiano

unread,
Nov 14, 2018, 3:45:12 AM11/14/18
to
The following program enter in my placemente new function only when the type is X.

#include <iostream>

struct X
{
};

void *operator new(size_t sz, X *p)
{
std::cout << "It's here with size " << sz << std::endl;
return p;
}

int main()
{
X v[10];

X *test = new(v) X;

std::cout << "------------------------------" << std::endl;

test = new(v) X[10];

return 0;
}

I tried several ways but I could not make the program access my function when I use the X[10].
How can I do it?

Bo Persson

unread,
Nov 14, 2018, 5:55:37 AM11/14/18
to
You would need a separate overload for 'operator new[]', to be used for
the array allocation.


Bo Persson

Jorgen Grahn

unread,
Nov 14, 2018, 6:56:29 AM11/14/18
to
Note that this stuff is rarely useful for most people. Last time I
overloaded operator new was 17 years ago. If you find yourself doing
it often (after learning how) then you're probably doing something
wrong.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Öö Tiib

unread,
Nov 14, 2018, 8:33:47 AM11/14/18
to
On Wednesday, 14 November 2018 10:45:12 UTC+2, Christiano wrote:

> I tried several ways but I could not make the program access my function when I use the X[10].
> How can I do it?

That is next to never needed. What you try to actually do?
Something like that will work:

#include <iostream>
#include <vector>

struct X {};

void *operator new(size_t sz, X* p)
{
std::cout << "Placement new to X* for size " << sz << "\n";
return p;
}

void *operator new[](size_t sz, X* p)
{
std::cout << "Placement new[] to X* for size " << sz << "\n";
return p;
}

int main()
{
std::vector<X> x(7707);

new(&x.front()) char;

std::cout << "----------------------------------\n";

new(&x.front()) char[42];

std::cout << "-W-O-R-K-S------------------------" << std::endl;
}

Note that without knowing difference between new and new[]
(and delete and delete[]) there are chances that you don't
understand even most mundane points of dynamic memory
management correctly. Read up there. Tricks like placement
news and even overriding such are likely too early to study.

Better leave *all* memory management to standard library
containers and smart pointers and avoid writing any C++ code
that contains keywords "new" or "delete" whatsoever. With
such code it is easier to become employed currently than
with seriously confused code.

Manfred

unread,
Nov 14, 2018, 9:27:02 AM11/14/18
to
On 11/14/2018 2:33 PM, Öö Tiib wrote:
> On Wednesday, 14 November 2018 10:45:12 UTC+2, Christiano wrote:
>
>> I tried several ways but I could not make the program access my function when I use the X[10].
>> How can I do it?
>
> That is next to never needed.
True.

What you try to actually do?
> Something like that will work:
>
> #include <iostream>
> #include <vector>
>
> struct X {};
>
> void *operator new(size_t sz, X* p)
> {
> std::cout << "Placement new to X* for size " << sz << "\n";
> return p;
> }
>
> void *operator new[](size_t sz, X* p)
> {
> std::cout << "Placement new[] to X* for size " << sz << "\n";
> return p;
> }
>
> int main()
> {
> std::vector<X> x(7707);
>
> new(&x.front()) char;
Also note the most confusing call of placement new to X* when given a
type 'char'.

>
> std::cout << "----------------------------------\n";
>
> new(&x.front()) char[42];
>
> std::cout << "-W-O-R-K-S------------------------" << std::endl;
> }
>
> Note that without knowing difference between new and new[]
> (and delete and delete[]) there are chances that you don't
> understand even most mundane points of dynamic memory
> management correctly. Read up there. Tricks like placement
> news and even overriding such are likely too early to study.
>
> Better leave *all* memory management to standard library
> containers and smart pointers and avoid writing any C++ code
> that contains keywords "new" or "delete" whatsoever. With
> such code it is easier to become employed currently than
> with seriously confused code.
>
Also true.
In my experience I have (rarely) used placement new, but never had to
override it.

Öö Tiib

unread,
Nov 14, 2018, 11:50:56 AM11/14/18
to
On Wednesday, 14 November 2018 16:27:02 UTC+2, Manfred wrote:
> On 11/14/2018 2:33 PM, Öö Tiib wrote:
> > On Wednesday, 14 November 2018 10:45:12 UTC+2, Christiano wrote:
> >
> >> I tried several ways but I could not make the program access my function when I use the X[10].
> >> How can I do it?
> >
> > That is next to never needed.
> True.
>
> What you try to actually do?
> > Something like that will work:
> >
> > #include <iostream>
> > #include <vector>
> >
> > struct X {};
> >
> > void *operator new(size_t sz, X* p)
> > {
> > std::cout << "Placement new to X* for size " << sz << "\n";
> > return p;
> > }
> >
> > void *operator new[](size_t sz, X* p)
> > {
> > std::cout << "Placement new[] to X* for size " << sz << "\n";
> > return p;
> > }
> >
> > int main()
> > {
> > std::vector<X> x(7707);
> >
> > new(&x.front()) char;
> Also note the most confusing call of placement new to X* when given a
> type 'char'.

That was my extension. OP code did placement new 'X' not 'char'.
I did it for to show that only the type of arguments inside () matters
to what placement new is picked by overload resolution and that
there won't be any diagnostics.

>
> >
> > std::cout << "----------------------------------\n";
> >
> > new(&x.front()) char[42];
> >
> > std::cout << "-W-O-R-K-S------------------------" << std::endl;
> > }
> >
> > Note that without knowing difference between new and new[]
> > (and delete and delete[]) there are chances that you don't
> > understand even most mundane points of dynamic memory
> > management correctly. Read up there. Tricks like placement
> > news and even overriding such are likely too early to study.
> >
> > Better leave *all* memory management to standard library
> > containers and smart pointers and avoid writing any C++ code
> > that contains keywords "new" or "delete" whatsoever. With
> > such code it is easier to become employed currently than
> > with seriously confused code.
> >
> Also true.
> In my experience I have (rarely) used placement new, but never had to
> override it.

I have seen it been overload for passing whatever other things
but the pointer for example for to instrument the buffer
somehow:

void* operator new [](size_t n, char c)
{
char* p = new char[n];
std::fill(p, p+n, c);
return p;
}

Such code always seems like deliberate obfuscation.
One likely has to read [expr.new] of standard to figure out
what is going on there and if and to what extent it is valid.

Manfred

unread,
Nov 14, 2018, 2:20:59 PM11/14/18
to
On 11/14/2018 5:50 PM, Öö Tiib wrote:
> On Wednesday, 14 November 2018 16:27:02 UTC+2, Manfred wrote:
>> On 11/14/2018 2:33 PM, Öö Tiib wrote:
[...]
>>>
>>> void *operator new(size_t sz, X* p)
>>> {
>>> std::cout << "Placement new to X* for size " << sz << "\n";
>>> return p;
>>> }
[...]
>>>
>>> new(&x.front()) char;
>> Also note the most confusing call of placement new to X* when given a
>> type 'char'.
>
> That was my extension. OP code did placement new 'X' not 'char'.
> I did it for to show that only the type of arguments inside () matters
> to what placement new is picked by overload resolution and that
> there won't be any diagnostics.
>
And, in fact the type of the arguments (X*) is used to resolve
overloading of the operator, and 'char' to deduce the size arg 'sz'
passed to the overloaded operator.
As I said, most confusing.

[...]
0 new messages