3

What is the difference between: new int*[] and new (int*)[]:

int** pA = new int*[2] // returns a pointer to an array of pointers to int.

new (int*)[2] // what should this return ? Shouldn't it be same as the above. 

Likewise,

float* pB1 = new float[3]; // compiles successfully.

float* pB2 = new (float)[3] //Gives an error. Shouln't it return an array of floats ?

But, the compiler says:

A value of type 'float' cannot be used to initialize a value of type float* .

What am I missing here? I'm using VS2015 Community IDE.

4
  • you are requesting a placement new; the call is understood as a pointer passed to new; see stackoverflow.com/questions/222557/… Commented Oct 11, 2017 at 18:58
  • en.cppreference.com/w/cpp/language/new Commented Oct 11, 2017 at 19:00
  • 1
    @OznOg, you should expand the comment to a full fledged answer. Commented Oct 11, 2017 at 19:00
  • Don't forget to use Standard Library containers like std::vector whenever possible and avoid manually allocating with new. Commented Oct 11, 2017 at 19:06

1 Answer 1

6
float* pB1 = new float[3];

is allocating an array of float

float* pB2 = new (float)[3]

is asking to allocate an array of ?? in 'float' location (which is meaningless), that's the error you get. This is the placement new syntax, see more info here Difference between new (float*)[] and new float*[] or http://en.cppreference.com/w/cpp/language/new as pointed in comments

Sign up to request clarification or add additional context in comments.

1 Comment

your answer contains a stack overflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.