1

I'm confused with the way to initial the pointer. Here is my code:

    int *p;
    int a = 5;
    new(p) int(a);

If it's the operator new function, how does the int(a) mean?Why can the statement achieve the initialization of the pointer?

sherlock
  • 11
  • 2
  • 2
    It is a [placement new](http://en.cppreference.com/w/cpp/language/new) expression, which creates an object of type `int` with the value of `a`, at the location `p`. But it doesn't initialize the pointer, `p` starts uninitialized, and stays uninitialized, so the code has undefined behaviour. – Jonathan Wakely Jun 29 '15 at 12:34
  • That is [placement new](https://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new) but @JonathanWakely is correct that it is undefined behavior too. – Cory Kramer Jun 29 '15 at 12:36

0 Answers0