Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

new or new[] operator

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[] ???

According 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[]) ?

like image 241
UmmaGumma Avatar asked Mar 20 '11 14:03

UmmaGumma


1 Answers

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.

like image 64
fredoverflow Avatar answered Sep 27 '22 21:09

fredoverflow