Difference of C / C ++ Array Declarations

4

What difference and impact do 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);
    
asked by anonymous 06.05.2017 / 19:31

2 answers

5

The first two are creating a crude array , the one that was used in C. In C ++ idiom you do not usually use this kind of array to talk with C code or who likes to mix two different languages.

In practice, the first is allocating memory in 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 you to increase or decrease the number of elements after it has been created, and it has methods that facilitate not only these operations but also a series of algorithms and utilities available in the class itself #

Only the second one works in C.

You can see more at Difference between std :: list, std :: vector and std :: array .

    
06.05.2017 / 19:54
3

First, the information provided here has been taken from this answer about using arrays in C ++. It pays to check for more complete information.

Automatic Array

int arr[n];

Automatic arrays (arrays that are usually in the stack ) are created each time the control flow passes through the line of code where it was defined. It is automatically deallocated 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 new T[size] (where T is the array type) syntax that will allocate the required space in memory and return a pointer to the first element. It can also be called an anonymous array.

This is an ASCII representation that describes the memory runtime of a dynamic array with 8 elements.

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

It is important to note that after finishing using an anonymous array, it must be deallocated to return the memory to the operating system, otherwise a memory leak will occur.

delete[] arr;

Vector

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

    
06.05.2017 / 21:39