Consider following code:
typedef SomeType type_t[2];
SomeType * arr1 = new type_t; //new or new[] ???
type_t * arr2 = new type_t[3]; //new or new[] ???
Accordi开发者_如何转开发ng standard which version of new
will be called in 1-st and 2-nd cases ( new
or new[]
) and how to delete arr1
and arr2
(with delete
or delete[]
) ?
First case allocates a one-dimensional array, second case a two-dimensional array. Both of them must be released via delete[]
, otherwise you will get undefined behavior.
It will use new[]
in both cases. You can verify this yourself by defining operator new[]
for SomeType
and printing something to the screen. You will see that it will be printed in both cases.
精彩评论