Dynamic memory allocation in C++

To run a C++ program:

The example of using dynamic memory allocation


#include 
#include  // need this to use malloc
using namespace std;

int main () {
  int n, i;
  int * numbers;

  cout << "Please enter a number: ";
  cin >> n;

  // allocate memory for n integers:
  numbers = (int *) malloc(n * sizeof(int));

  for (i = 0; i < n; ++i) {
    numbers[i] = i;

  }

  for (i = 0; i < n; ++i) {
    cout << numbers[i] << " ";
  }

  cout << endl;
  // free the memory at the end
  free(numbers);

}

CSci 4651 course.