Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
In the first case, memory is allocated on the heap and must be explicitly freed by calling delete[] arr; when the array is no longer needed. In the second, memory is allocated on the stack and will be freed after leaving the scope of the arr variable.
Еще одно важное различие: в первом случае мы можем задавать размер массива динамически, в процессе работы программы:
int size = 0;
cout << "Введите размер массива: "; cin >> size;
int *arr = new int[size];
const int SIZE = 333 + 768 * 2;
int arr[SIZE];
In the first case, arr is a pointer. A pointer is a variable that stores some address of some data of a certain size. A pointer can be changed by assigning new values to it. It can be shifted to the right (arr++) and left (arr--) or even rearranged to some other data in a different place (arr = new address). So the pointer can be put at the beginning of the array, or you can put it in the middle of the array, or you can put it at the end of the array and then read or change the array in that place through it.
In the second case, arr is an array. As you created it, so it remains to the end.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question