Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between new (float*)[] and new float*[]

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.

like image 443
Nibedita Banerjee Avatar asked Sep 17 '26 21:09

Nibedita Banerjee


1 Answers

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

like image 129
OznOg Avatar answered Sep 19 '26 13:09

OznOg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!