OK here goes file mou... I will try and explain the difference without
getting too technical,
Declaring an array as "double Array1[10] can be thought of as declaring
a "hard coded" array... i.e. it's size MUST be known at the time of
compilation.... For example,
const int CONST_VAL = 5;
int nVal = 6;
double Array1[10]; // valid
double Array2[CONST_VAL]; // also valid
double Array3[nVal]; // NOT valid, nVal is not known
at compilation!!
Declaring an array as "double* pArray2 = new double[10]" has the same
effect with the main difference being the size of the array can be
stated from the value contained in a variable.... For example,
const int CONST_VAL = 5;
int nVal = 6;
double* pArray1 = new double[10]; // valid
double* pArray2 = new double[CONST_VAL]; // also valid
double* pArray3 = new double[nVal]; // also valid
Whichever way the array is created, it is still accessed in the same
way
double Array1[10]; // Array 1
double pArray2 = new double[10]; // Array 2
for( int i = 0; i < 10; i++ )
{
Array1[ i ] = i;
pArray2[ i ] = i;
}
Both arrays will contain exactly the same data...
BUT remember, any memory created using the "new" keyword MUST be
deleted when finished with otherwise you risk aquiring memory
leaks!!... And also "hard coded" arrays do not require deletion as they
are automatically deleted when out of scope!!...
So Array 2 requires to be deleted as so when not required further,
delete pArray2; (or delete [] pArray2;)
Regards
Frinos
Vasileios Zografos wrote:
Hello,
can anyone please tell me if there is any difference between the two:
double Array1[10];
and
double* Array2 = new double[10];
.
.
.
delete [] Array2;
Thanks