Difference in C/C++ array declarations

Asked

Viewed 602 times

6

What is the difference and impact that each of these 3 vector statements bring to my code?

int n; 
cin >> n;
int* arr = new int[n];

int n; 
cin >> n;
int arr[n];

int n;
cin >> n;
vector <int> arr(n);

2 answers

7


The first two are creating a array crude, the one that was used in C. In idiomatic code C++ this type of array except to chat with C code or who likes to mix two different languages.

In practice, the former is allocating memory in the heap (new) and while the second is allocating memory in the stack. Understand the difference in What are and where are the "stack" and "heap"?.

The third uses a typical C++ data structure. It allows increasing or decreasing the number of elements after it has been created, and relies on methods that facilitate not only these operations but also a series of algorithms and utilities available in the class itself vector or other components of the standard library or third parties that use the protocols adopted by the common C++ library structures. It should be preferred whenever possible, until it proves necessary to use something different.

Only the second works in C.

You can see more in Difference between Std::list, Std::vector and Std:array.

  • I don’t know if it’s right to make these statements about the heap and stack since the C++ standard does not guarantee this. Check out the answers to this other topic: http://stackoverflow.com/questions/6159968/declaring-array-of-int#comment7157068_6159973

  • I am giving a practical response to what actually happens in all compilers today, in the past, and that is what will happen in the future unless there is a revolution that no one has ever considered, what will surely bring about such a stir that almost everything that was said before will be invalidated. I’m just considering the long shot. In your answer you also have inadequate terms, but even so it is not wrong or confusing people for all intents and purposes. Has situation to give response based on specification, has situation to explain in broad lines.

  • There in the pointed comments are talking about how pedantic it is to cling to it in something so simple. The difference is that the answer you copied did not appear anyone pedantic to complain.

  • Just making it clear I didn’t mean to belittle your answer. That’s why I said I didn’t even know if it was right to make those statements (in other words, if it’s worth looking at those measly details). I even asked because I’m not an experienced member and so, any teaching is valid.

  • In addition, the answer was not a pure and dumb copy. A lean adaptation was made and credits were given.

5

First, the information provided here has been removed of this answer (in English) about the use of C++ arrays. It is worth checking for more complete information.

Automatic Array

int arr[n];

The automatic arrays (arrays that are normally in the stack) are created each time the control flow passes through the code line where it was defined. It is automatically displaced when the control flow reaches the end of the scope where it was created.

Note that the created variable stores the address for the first element of the array.

Edit: putting the representation in ASCII.

                 +---+---+---+---+---+---+---+---+
                 |   |   |   |   |   |   |   |   |  
                 +---+---+---+---+---+---+---+---+
                   ^
                   |
                  arr

Dynamic Array

int* arr = new int[n];

Dynamic array has no name, so the only way to access it is through the pointer. In C++ they are created using the syntax new T[size] (where T is the type of the array) that will allocate the required space in memory and return a pointer to the first element. It can also be called an anonymous array.

Here is an ASCII representation that describes the running-time memory operation of an 8-element dynamic array.

                 +---+---+---+---+---+---+---+---+
(array anônima)  |   |   |   |   |   |   |   |   |  (new int[8])
                 +---+---+---+---+---+---+---+---+
                   ^
                   |
                   |
                 +-|-+
       int* arr  | | |                       
                 +---+

It is important to note that after you have finished using an anonymous array, it should be de-located to return the memory to the operating system, otherwise there will be a memory leak (memory Leak).

delete[] arr;

Vector

The class vector, which comes by default in C++ (see STL), is an alternative to primitive array representation (like the previous two). It comes with some extra functions, in addition to being dynamically allocated, which allows adding or removing elements even after being declared.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.