P
pedicini
I work with many dynamically allocated variables in
my program including double **, int *, char *.
For ex:
double **d;
d = new (double *) [10];
for(int i = 0; i < 10; i++) {
d = new double[10];
}
to setup a 2d array of doubles.
My memory clear function looks like this:
clearDoubleArray(double **d, int rows)
{
for(int i = 0; i < rows; i++) {
delete [] d;
}
delete [] d;
d = NULL;
}
In my program, the same variable is allocated and deallocated a great deal
of times.
Sometimes I'm not sure whether d has been initialized, or has been deleted.
Is it safe
to call clearDoubleArray() on a var that has not been allocated? Or should I
keep track?
Just for the sake of argument, assume that whenever d has not been
allocated, that rows = 0,
so it does not use the delete[] d loop shown above.
Will delete[] d cause problems?
Also, I try to set d to NULL on the last statement of the function, but it
does not change
where d is pointing to. How can I fix that?
Thanks
Shaun
my program including double **, int *, char *.
For ex:
double **d;
d = new (double *) [10];
for(int i = 0; i < 10; i++) {
d = new double[10];
}
to setup a 2d array of doubles.
My memory clear function looks like this:
clearDoubleArray(double **d, int rows)
{
for(int i = 0; i < rows; i++) {
delete [] d;
}
delete [] d;
d = NULL;
}
In my program, the same variable is allocated and deallocated a great deal
of times.
Sometimes I'm not sure whether d has been initialized, or has been deleted.
Is it safe
to call clearDoubleArray() on a var that has not been allocated? Or should I
keep track?
Just for the sake of argument, assume that whenever d has not been
allocated, that rows = 0,
so it does not use the delete[] d loop shown above.
Will delete[] d cause problems?
Also, I try to set d to NULL on the last statement of the function, but it
does not change
where d is pointing to. How can I fix that?
Thanks
Shaun