When an array is created on the heap inside a function is it necessary to delete the array in the main function? Consider, this program:
#include "stdafx.h"
#include <iostream>
using namespace std;
int * return_array() {
int* my_array = new int[10];
my_array[0] = 10;
return my_array;
}
int main()
{
int * returned_array = return_array();
cout << returned_array[0];
delete[] returned_array;
return 0;
}
This line: int* my_array = new int[10];
is it necessary to delete in the main program? I find that doing this every time will hard to maintain and get right.