Constructor's usage

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// operator new example
#include <iostream>
#include <new>
using namespace std;

struct myclass {myclass() {cout <<"myclass constructed\n";}
                         ~myclass() {cout << "myclass destroyed\n";}};

int main () {
   myclass * p3 = (myclass*) operator new (sizeof(myclass));
// (!) not the same as:
// myclass * p3 = new myclass;
// (constructor not called by function call, even for non-POD types)

   new (p3) myclass;   // calls constructor //- Thank @Albatross's syntax below
// same as:
// operator new (sizeof(myclass),p3)

   
   delete p3;
   // OR: p3->~myclass();          //- Thank @Cubbi
  //        operator delete (p3);   //- Thank @vlad_from_moscow
   return 0;
}


Environment: Ubuntu Server x64 9.10; G++.
Language: C++.

Hi guys,
This is my first time to post my question here. Thank you for your time.

My question is the line of ( new (p3) myclass; ). When I change the line to ( (*p3).myclass();), compiler says my usage of myclass is invalid. I just don't know the usage of the ( new (p3) myclass; ). If you know why, please tell me or share with me your opinion.

Thank you :)
Last edited on
¿what language do you come from?

1
2
3
int main(){
  myclass p3; //creates an object (it calls the constructor)
}
Would giving you the syntax of placement new answer your question?
new (pointer_to_allocated_memory) name_of_constructor;
The allocated buffer has to be at least big enough to store your object.

EDIT @ne555:
That's actually valid C++, albeit very rarely used. Even I don't use pure memory allocation combined with placement new.

Fine print: the more unusual news are very easy to abuse and mess up, more so than the regular news. Please don't use them unless you know what you're doing. Case and point that code has a memory leak.

-Albatross
Last edited on
I think that there is some ambiguous in interpreting this constructor ( (*p3).myclass();), by the compiler. If you will write

( *p3 ).myclass::myclass();

then the code will be compiled.
Last edited on
closed account (zb0S216C)
Make sure you call the correct delete form. Note that delete p3 is not correct.

1
2
int *Memory(static_cast<int *>(operator new(sizeof(int))));
operator delete (Memory);

Wazzak
Hi @ne555:

I know this usage is a little bit awkward. It is a valid C++ :)


Hi @Albatross:

Thank you for your detailed explain. I don't use "new" as that way neither. Just curious about the usage :P . Your answer helps my lots. Appreciate!

Hi @vlad_from_moscow:

I tried your way, but it doesn't work on my machine :S. Probably results from different compilers' core.

Hi @framework:
Thank you for your remind. I will add it to my code. BTW, I find "delete p3" works on my machine if I add a Destructor within code. For your method, I don't know how to use your code :S

Alex
Last edited on
If you used placement new to construct an object, you should call the destructor directly:
1
2
3
4
5
6
int main () {
   myclass * p3 = (myclass*) operator new (sizeof(myclass)); // allocates
   new (p3) myclass;   // calls constructor
   p3->~myclass(); // calls destructor
   operator delete(p3); // deallocates
}

But really, it's not a particularly useful part of the language. Placement new is almost always hidden from view by std::allocator and other layers of the standard library.
Last edited on
Hi @Cubbi,

Thank you, the codes should be fine now :)
Topic archived. No new replies allowed.