1

No matter what syntax I try, this code is always a compile error:

int (*foo)[3] = new ???;

I've tried

int (*foo)[3] = new (int[3]);
int (*foo)[3] = new (int(*)[3]);
int bar[3];
int (*foo)[3] = new decltype(bar);

1 Answers1

3

Well, it's easy.

int (*foo)[3] = new int[1][3];
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    Ah, of course. Is it possible to make it deletable with `delete` instead of `delete[]`? – Artikash-Reinstate Monica Feb 20 '19 at 22:04
  • @Artikash Anything allocated with `new` is "deletable". The key here is to figure out how to delete it. – Code-Apprentice Feb 20 '19 at 22:10
  • I am trying `int (*f)[3] = new int[3];` but the `new int[3]` decays from array to pointer. I don't thing using `delete` is possible - even if you can allocate memory, you would allocate an array of 3 integers and store the pointer to it in `foo`. That's still an array. – KamilCuk Feb 20 '19 at 22:10
  • You can allocate the memory for pointer itself `int (**foo)[3] = new (int(*)[3]);` that would free with delete. – KamilCuk Feb 20 '19 at 22:13
  • My guts tells me `int (*foo)[3] = reinterpret_cast(new int[3])` should be ok, because it's the same as `int mem[3]; int (*foo)[3] = reinterpret_cast(mem);` and `&mem` and `mem` have the same value (different type)... – KamilCuk Feb 20 '19 at 22:20