1
cin>>n; 

int * a;
a=new int[n];

int b[n];

I think in both the cases arrays are created dynamically(correct me).So why don't we prefer 2nd case over first because 2nd is shorter and easy.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
hoder
  • 257
  • 1
  • 3
  • 14

3 Answers3

4

The second one does not work in standard C++. It does however work in C. Some compilers offer extensions (C++) that do allow this, but it's, again, not standard. The C++14 will come with std::dynarray which is basically what the second "way" does.

ScarletAmaranth
  • 5,065
  • 2
  • 23
  • 34
  • C++14 will also come with straight VLAs, but both them and `dynarray` are in a TS. – chris Nov 24 '13 at 20:36
  • … or maybe C++14 will come without either of them. I am not sure of the current state of affairs, but some issues were raised with both `dynarray` and the equivalent of VLAs (that has a different name I don't recall) – David Rodríguez - dribeas Nov 24 '13 at 20:40
  • @DavidRodríguez-dribeas, As far as I know, they were just moved to a TS and that seemed pretty final. I guess things could still change, though. – chris Nov 24 '13 at 21:00
0

Because it is misleading.

int b[n] is used for static array declaration. And in principle is not allowed in C++ (in case n is not constant), it is just compiler extension, which does all work for you converting int b[n] into int *b = new int[n].

In such cases better to use std::vector, which is also short, but does not mislead:

vector<int> b(n);
klm123
  • 12,105
  • 14
  • 57
  • 95
  • `int b[n]` is *not* a static array when declared inside a function, and `int b[n]` is *not* the same as `*b = new int[n]`. The former is a stack allocation, and the latter is a heap allocation. – Jim Buck Nov 24 '13 at 20:32
  • @JimBuck, are you talking about C++? Could you give me links, which says the same? – klm123 Nov 24 '13 at 20:39
  • If the compiler transformed it into a dynamic allocation, it would be a bit of a pointless extension. You can read the specifics of how it works in each compiler's documentation. – chris Nov 24 '13 at 21:01
  • @klm123 - I'm talking about C and C++, though of course there is no `new` in C, but `int b[n];` is a stack allocation in both languages. Search on the internet for "stack allocation". @chris nailed it. – Jim Buck Nov 25 '13 at 05:10
0

One important difference is that the first one allocates memory in the heap and, with the proper reference, can be accessed in all the program. An should be freed once is not references (with corresponding call to delete).

The second one allocates it in the stack and, as you can check at Why no VLAs in C++0x? this is one of main complains against VLAs. This memory is freed once you exit current block.

On the other hand, been strict, neither of them are dynamic arrays (you can't add/remove extra elements dynamically). If you want to work with dynamic arrays I would suggest using std::vector. Although, more fitting to provided example, would be using std::array

jcm
  • 2,568
  • 14
  • 18