开发者

new or new[] operator

开发者 https://www.devze.com 2023-02-18 03:48 出处:网络
Consider following code: typedefSomeType type_t[2]; SomeType * arr1 = new type_t; //new or new[] ??? type_t * arr2 = new type_t[3]; //new or new[] ???

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消