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

how apply the default constructor without the sys call the destructor bug manualy call that destructor too

67 views
Skip to first unread message

Rosario19

unread,
Jun 13, 2018, 1:45:19 PM6/13/18
to
i have one region of memory returned from malloc
i would like the default constructor for type T (can be all type, from
int to thistype) is applied to that memory manually without the sys
automatic call the destructor

example something as

T *p=malloc(sizeof(T));

if(p)
{T::p();// call manually the default void argument constructor for p
... // code using p
~T::p(); // call manually the default destructor for p
}

but this not compile

how to do it? is possible using the default constructor of one object
on memory?

Rosario19

unread,
Jun 13, 2018, 1:48:42 PM6/13/18
to
On Wed, 13 Jun 2018 19:49:40 +0200, Rosario19 wrote:

Re: how apply the default constructor without the sys call the
destructor but manualy call that destructor

>i have one region of memory returned from malloc
>i would like the default constructor for type T (can be all type, from
>int to thistype) is applied to that memory manually without the sys
>automatic call the destructor
>
>example something as
>
>T *p=malloc(sizeof(T));
>
>if(p)
> {T::p();// call manually the default void argument constructor for p
> ... // code using p
> ~T::p(); // call manually the default destructor for p
> }

T *p=malloc(sizeof(T));

if(p)
{T::(*p)();// call manually the default void arg constructor for p
... // code using p
~T::(*p)(); // call manually the default destructor for p

Chris Vine

unread,
Jun 13, 2018, 3:01:49 PM6/13/18
to
On Wed, 13 Jun 2018 19:49:40 +0200
You should use placement new:
https://en.cppreference.com/w/cpp/language/new#Placement_new

Rosario19

unread,
Jun 14, 2018, 5:51:27 AM6/14/18
to
On Wed, 13 Jun 2018 20:01:34 +0100, Chris Vine wrote:
>On Wed, 13 Jun 2018 19:49:40 +0200
>Rosario19 <R...@invalid.invalid> wrote:
>> i have one region of memory returned from malloc
>> i would like the default constructor for type T (can be all type, from
>> int to thistype) is applied to that memory manually without the sys
>> automatic call the destructor
>>
>> example something as
>>
>> T *p=malloc(sizeof(T));
>>
>> if(p)
>> {T::p();// call manually the default void argument constructor for p
>> ... // code using p
>> ~T::p(); // call manually the default destructor for p
>> }

i imagined

if(p)
{T::p();// call manually the default void arg constructor for p
... // code using p
~T::p(); // call manually the default destructor for p
}

or
if(p)
{p.();// call manually the default void arg constructor for p
... // code using p
p.~(); // call manually the default destructor for p
}

but they are wrong

>> but this not compile
>>
>> how to do it? is possible using the default constructor of one object
>> on memory?

>You should use placement new:
>https://en.cppreference.com/w/cpp/language/new#Placement_new

thank you very much, appear i have not idioms of C++ so would be this:

T *p=malloc(sizeof(T));
if(p)
{p->T(); // call manually the default void arg constructor for p
... // code using p and *p
p->~T();// call manually the default destructor for p
}

and the sys not automatic call it... this can be useful for doing
arrays of type T using malloc


Rosario19

unread,
Jun 14, 2018, 5:53:16 AM6/14/18
to
On Thu, 14 Jun 2018 11:55:49 +0200, Rosario19 wrote:


>T *p=malloc(sizeof(T));
>if(p)
> {p->T(); // call manually the default void arg constructor for p
> ... // code using p and *p
> p->~T();// call manually the default destructor for p
> }


T *p=malloc(sizeof(T));
if(p)
{p->T(); // call manually the default void arg constructor for p
... // code using p and *p
p->~T();// call manually the default destructor for p
free(p);
}

red floyd

unread,
Jun 14, 2018, 12:54:03 PM6/14/18
to
** *READ* ** the link that Chris gave you. It gives you the proper
syntax for placement new and delete.


red floyd

unread,
Jun 14, 2018, 12:56:03 PM6/14/18
to
The other thing is that placement new is almost ALWAYS the wrong thing
to do. If all you are trying to do is allocate a T dynamically, then
just do

T* p = new T;

Rosario19

unread,
Jun 15, 2018, 1:25:03 AM6/15/18
to
On Thu, 14 Jun 2018 11:57:38 +0200, Rosario19 wrote:

>On Thu, 14 Jun 2018 11:55:49 +0200, Rosario19 wrote:
>
>
>>T *p=malloc(sizeof(T));
>>if(p)
>> {p->T(); // call manually the default void arg constructor for p
>> ... // code using p and *p
>> p->~T();// call manually the default destructor for p
>> }
>
>
>T *p=malloc(sizeof(T));
>if(p)
> {p->T(); // call manually the default void arg constructor for p

appear that above not compile as not compile (*p).T()


> ... // code using p and *p
> p->~T();// call manually the default destructor for p

appear that above compile ok and link ok

> free(p);
> }

my workaround

T *p=malloc(sizeof(T));

if(p)
{u8 *pc;
u32 i;
pc=(u8*) p; for(i=0;i<sizeof(T);++i) pc[i]=0;
.....
(*p).~T();
free(p);
}

this because in my memory, out the possible case of double (or float
point)
the obj all 0, is one valid obj that has to be initializated and other
operator (as operator=, can initialize it ok)

Rosario19

unread,
Jun 15, 2018, 1:28:47 AM6/15/18
to
On Thu, 14 Jun 2018 09:55:52 -0700, red floyd wrote:

>The other thing is that placement new is almost ALWAYS the wrong thing
>to do. If all you are trying to do is allocate a T dynamically, then
>just do
>
>T* p = new T;

ci dovrebbe esser un proverbio

"preferisco sempre quello che ho scritto"

perche' quello che ho scritto lo posso correggere
conosco come trattarlo,
ma quello che hanno scritto gli altri nn lo posso correggere
e nn lo conosco

Rosario19

unread,
Jun 15, 2018, 1:33:25 AM6/15/18
to
On Fri, 15 Jun 2018 07:29:28 +0200, Rosario19 wrote:

>T *p=malloc(sizeof(T));
>
>if(p)
> {u8 *pc;
> u32 i;
> pc=(u8*) p; for(i=0;i<sizeof(T);++i) pc[i]=0;
> .....
> (*p).~T();
> free(p);
> }

this above compile run and not leak

>this because in my memory, out the possible case of double (or float
>point)
>the obj all 0, is one valid obj that has to be initializated and other
>operator (as operator=, can initialize it ok)

if the obj has one element that has the len of memory of its sub obj
and that element is 0, than it is called malloc() for give memory
at last in all class i wrote using malloc() until now

Öö Tiib

unread,
Jun 15, 2018, 1:59:25 AM6/15/18
to
If you don't care what others answer then why to write the
question "how to do it?" here at the first place?
Every regular in this group knows how to do it but it is
pointless to answer that to you.

Chris Vine

unread,
Jun 15, 2018, 7:50:14 AM6/15/18
to
On Fri, 15 Jun 2018 07:29:28 +0200
You have two problems. First your postings are nearly unintelligible.
Why not try using something like Google translate which may make a
better fist of it? It is worth checking it out to see.

Secondly and more importantly, you have already been given the answer,
namely to use placement new, which is specifically intended to deal
with this case, namely a case where an object is to be constructed in
pre-allocated raw memory, such as has been returned by malloc(). The
form is this:

void* p = malloc(sizeof(T));
T* tp = new (p) T;
... do something with tp ...
tp->~T();
free(tp);

There is even a similar example on the link I gave you. Your approach
does not work and demonstrates sheer pig-headed stupidity in the face
of your attention having been drawn to the correct tool for the job.
You think you know better. I am afraid that that is a grave over-
estimation of your abilities.

Rosario19

unread,
Jun 16, 2018, 4:50:36 AM6/16/18
to
i not use new() operator only malloc() or malloc like functions
the new() operator i have is not the new() library provide

>namely a case where an object is to be constructed in
>pre-allocated raw memory, such as has been returned by malloc(). The
>form is this:
>
> void* p = malloc(sizeof(T));
> T* tp = new (p) T;

this line what would make?
call new, pass it the pointer to void* p
call the constructor in the address of p of the compatible T
constructor?

> ... do something with tp ...
> tp->~T();
> free(tp);

this above seems to me better free(p)

>There is even a similar example on the link I gave you. Your approach
>does not work and demonstrates sheer pig-headed stupidity in the face
>of your attention having been drawn to the correct tool for the job.

in the start of the class, that use the generic T type
i could write something as
"this is ok only if type T allow the existence of one element that has
in it all memory 0"
this is equvalent to the "call one constructor on the type when the
memory exist"

Rosario19

unread,
Jun 16, 2018, 4:57:39 AM6/16/18
to
this is not true, 80% of C++ is out of my know, so i need some answer
to question as "how call one constructor in the memory return from
malloc()?"

Chris Vine

unread,
Jun 16, 2018, 7:37:58 AM6/16/18
to
On Sat, 16 Jun 2018 10:55:02 +0200
Rosario19 <R...@invalid.invalid> wrote:
> On Fri, 15 Jun 2018 12:50:00 +0100, Chris Vine wrote:
> i not use new() operator only malloc() or malloc like functions
> the new() operator i have is not the new() library provide

No, you have got something completely wrong, although it is difficult to
decipher your remarks to find out what. operator new() either
allocates and returns raw memory, or (when it takes a second void*
argument in connection with a placement new expression) does nothing.
In many implementations, an allocating operator new() comprises no more
than a call to malloc(). You seem to be confusing operator new() with
the new expression, which is something different. operator new()
returns raw uninitialized memory. The new expression constructs an
object in that memory.

> >namely a case where an object is to be constructed in
> >pre-allocated raw memory, such as has been returned by malloc(). The
> >form is this:
> >
> > void* p = malloc(sizeof(T));
> > T* tp = new (p) T;
> this line what would make?
> call new, pass it the pointer to void* p
> call the constructor in the address of p of the compatible T
> constructor?

It constructs an object of type T, via T's constructor, in the memory
pointed to by p, rather than calling allocating operator new() to
obtain a dynamic memory allocation and constructing it there.

> > ... do something with tp ...
> > tp->~T();
> > free(tp);
>
> this above seems to me better free(p)

No, it makes no difference. The value of p and tp is the same, but one
is of type void* and the other of type T*. Since free() takes its
argument by void* it makes zero difference.

> in the start of the class, that use the generic T type
> i could write something as
> "this is ok only if type T allow the existence of one element that has
> in it all memory 0"
> this is equvalent to the "call one constructor on the type when the
> memory exist"

This is unintelligible. Possibly you asked the wrong question and you
are not interested in placement new. Possibly you want to install your
own global operator new() for your program which always hands off to
malloc() (although that is probably what your default global operator
new() already does anyway), or possibly you want to override global
operator new for a particular class. The latter is pretty common for
types that you want to implement their own memory pools. You still use
the new expression to construct objects of such classes, but when the
new expression obtains new memory it calls the class specific version of
operator new() instead of global operator new().

Try getting a decent book on C++.

Öö Tiib

unread,
Jun 16, 2018, 9:36:23 AM6/16/18
to
I see that Chris Vine already explained to you that with placement new.

// allocate memory
void* p = malloc(sizeof(T));

// check that allocation worked
if (p == nullptr)
return outOfMemory;

// construct T into that memory
T* tp = new (p) T(arguments, ofTs, constructor);

// ... use tp, now it points at T object ...

// destroy T
tp->~T();

// construct other T into same memory
T* tp = new (p) T(otherArguments, ofTs, constructor);

// ... use tp, now it points at different T object in same memory ...

// destroy T
tp->~T();

// free the memory
free(tp);

Rosario19

unread,
Jun 16, 2018, 12:46:02 PM6/16/18
to
On Sat, 16 Jun 2018 12:37:23 +0100, Chris Vine wrote:

>Try getting a decent book on C++.

all yours example, in this compiler: not compile
"T *pt=new(p)T;" or "T *pt=new(p)T();" or "T*pt=new(p)T(parameters);"
... possible this compiler it is a little old
or i not link any standard sys new or delete...

but the situation it is easier and clearer thand one all you propose

The step for resolve the situation are

1) write in the source code of the dll one use
void* _export operator new(size_t sz){return yourMalloc(sz);}
void _export operator delete(void*p){yourfree(p);}

2) link that .dll and not some other has different new or delete

write in your favorite code using memory your allocator return and
free it. example

template <class> i32 prova(T& a)
{T *p;

// this below i had seen
//1) call yourMalloc() function with argument sizeof(T)
//2) call the default void constructor inthat memory for type T to*p
//3) assing to p the address return yourMalloc
p=new T;

*p=a;
ooo<<"this is a:"<<*p<<"\n";

// this below i had seen
// 1) apply to *p the destructor for T
// 2) call yourfree(p) for free the memory
delete p;
}

so no memory leak and memory it seems correct used
-------------------------------

in the dll had seen too
void * _export operator new[](size_t sz){return yourMalloc(sz);}
void _export operator delete[](void* pv){yourFree(pv);}

but i have some doubit they are wrong....
are these right or wrong?

Rosario19

unread,
Jun 16, 2018, 12:48:32 PM6/16/18
to
On Sat, 16 Jun 2018 06:36:06 -0700 (PDT), 嘱 Tiib wrote:

here this

> // construct T into that memory
> T* tp = new (p) T(arguments, ofTs, constructor);

not compile
the same "p=malloc(sizeof(T));T* tp=new(p)T();"
not compile

Rosario19

unread,
Jun 16, 2018, 12:54:29 PM6/16/18
to
"T *p=(T*)malloc(sizeof(T));T* tp=new(p)T();"

not compile

Öö Tiib

unread,
Jun 16, 2018, 1:07:05 PM6/16/18
to
But it has been in C++ for ages wher you got so old compiler?
Oh, sorry, forgot to tell that you need to #include <new>
Also it seems that the cppreference does not state that anywhere.
No wonder that you were confused.

Otherwise it shoud work in C++2003 at least. Demo here:
http://coliru.stacked-crooked.com/a/0a4465d8d6d90e0b
Compiles and runs.

Rosario19

unread,
Jun 16, 2018, 1:14:15 PM6/16/18
to
On Sat, 16 Jun 2018 10:06:51 -0700 (PDT), 嘱 Tiib <oot...@hot.ee>
wrote:

>On Saturday, 16 June 2018 19:54:29 UTC+3, Rosario19 wrote:
>> On Sat, 16 Jun 2018 18:53:01 +0200, Rosario19 wrote:
>>
>> >On Sat, 16 Jun 2018 06:36:06 -0700 (PDT), ? Tiib wrote:
>> >
>> >here this
>> >
>> >> // construct T into that memory
>> >> T* tp = new (p) T(arguments, ofTs, constructor);
>> >
>> >not compile
>> >the same "p=malloc(sizeof(T));T* tp=new(p)T();"
>> >not compile
>>
>> "T *p=(T*)malloc(sizeof(T));T* tp=new(p)T();"
>>
>> not compile
>
>But it has been in C++ for ages wher you got so old compiler?
>Oh, sorry, forgot to tell that you need to #include <new>

yes i not include any "<new>" header
it would be for this

Chris Vine

unread,
Jun 16, 2018, 2:39:26 PM6/16/18
to
On Sat, 16 Jun 2018 18:50:25 +0200
Rosario19 <R...@invalid.invalid> wrote:
> On Sat, 16 Jun 2018 12:37:23 +0100, Chris Vine wrote:
>
> >Try getting a decent book on C++.
>
> all yours example, in this compiler: not compile
> "T *pt=new(p)T;" or "T *pt=new(p)T();" or "T*pt=new(p)T(parameters);"
> ... possible this compiler it is a little old
> or i not link any standard sys new or delete...

As I said:

"Try getting a decent book on C++."

If you had done so, it would have told you to include the <new> header
in order to use placement new. (Also include this header if you want
to override global or class operator new()/delete() - see below.)

> but the situation it is easier and clearer thand one all you propose
>
> The step for resolve the situation are
>
> 1) write in the source code of the dll one use
> void* _export operator new(size_t sz){return yourMalloc(sz);}
> void _export operator delete(void*p){yourfree(p);}
[More of the same elided]

As I said:

"Possibly you asked the wrong question and you are not interested in
placement new. Possibly you want to install your own global operator
new() for your program which always hands off to malloc() (although
that is probably what your default global operator new() already does
anyway), or possibly you want to override global operator new for a
particular class."

Instead of being a total dick and ignoring all the advice you have been
given by at least two people who know what they are talking about,
consider this:

1. Not everyone else is an idiot. Possibly you have it wrong and you
are the idiot.

2. When someone gives you advice, think about it and read up about it,
because you are probably wrong and they are probably right.

3. If you are a beginner, adopt a better attitude and you will learn
more.

Rosario19

unread,
Jun 16, 2018, 3:10:20 PM6/16/18
to
On Sat, 16 Jun 2018 18:50:25 +0200, Rosario19 wrote:

>in the dll had seen too
>void * _export operator new[](size_t sz){return yourMalloc(sz);}
>void _export operator delete[](void* pv){yourFree(pv);}
>
>but i have some doubit they are wrong....
>are these right or wrong?

no doubit

i debugged these above, at last delete[], possible i'm not smart
enought but for me they are really miracolous
how correctly free all and call recursivly the destructors of
subtypes...
no leak nothing wrong...thank you C++, thank you to all...

new and delete are not simple operators they seems something more, as
one infrastructure one sys (they are not the code one see)

i had seen in the debugger somehting as

thisType *p;

p=new thisType[10];
....
delete p;


Rosario19

unread,
Jun 16, 2018, 3:13:29 PM6/16/18
to
On Sat, 16 Jun 2018 21:14:48 +0200, Rosario19 <R...@invalid.invalid>
wrote:
0 new messages