UmmaGumma
UmmaGumma

Reputation: 5693

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

Upvotes: 8

Views: 449

Answers (2)

sepp2k
sepp2k

Reputation: 370435

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.

Upvotes: 1

fredoverflow
fredoverflow

Reputation: 263350

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.

Upvotes: 9

Related Questions