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.

6
  • 1
    you need to learn how memory management in C++ works. i.e. memory in heap allocated by new and memory in stack. Commented Nov 24, 2013 at 20:18
  • 1
    The second one wouldn't work. Commented Nov 24, 2013 at 20:18
  • like Barmaley said - why even ask a question about something that straight up won't compile at all? Commented Nov 24, 2013 at 20:20
  • It compiled on my old compiler. Commented Nov 24, 2013 at 20:23
  • the keyword here is old ;) Commented Nov 24, 2013 at 20:23

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

C++14 will also come with straight VLAs, but both them and dynarray are in a TS.
… 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)
@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.
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);

4 Comments

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.
@JimBuck, are you talking about C++? Could you give me links, which says the same?
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.
@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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.