I'm simulating the propagation of waves in 3D, to step a distance dz requires the solution of a system of linear equations of the form A.x^{new} = B.x^{old}, where x represents the shape of the wave, and A, B are large-ish N*N arrays of type double. The process is represented by
To update A and B at each step I allocate the memory required and fill them with the necessary values. Once I've solved the system I de-allocate A and B and loop over the process.
I've experienced no problems with this process so far, but I'm wondering if this strategy of repeated allocation / de-allocation is optimal.
Would it be better to allocate the memory required once, at the start of the loop, and then de-allocate once at the end?
Does it make any difference as long as I am de-allocating the memory correctly?
What does bytes recommend?
Thanks.
Code:
int Nsteps, Npos;
double dz;
// arrays to hold the field values
double *x_old;
double *x_new;
// arrays to hold the matrices for the system A.x^{new} = B.x^{old}
double **A;
double **B;
// Assume x^{old} is defined by initial input
define_x_initial();
for(int i=1; i<=Nsteps; i++){
// Define the matrices A and B
define_A();
define_B();
// Define x^{new} by solving the system
solve_system();
}
I've experienced no problems with this process so far, but I'm wondering if this strategy of repeated allocation / de-allocation is optimal.
Would it be better to allocate the memory required once, at the start of the loop, and then de-allocate once at the end?
Does it make any difference as long as I am de-allocating the memory correctly?
What does bytes recommend?
Thanks.
Comment