Repeated dynamic memory allocation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emaghero
    New Member
    • Oct 2006
    • 85

    #1

    Repeated dynamic memory allocation

    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

    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();
    }
    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.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    If there's no need to dynamically allocate memory, then I would avoid it. If you can simply reuse the array by reinitializing it, that would be the preferable choice because that's just less overhead.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      So how are the matrices A and B allocated? In the code A and B are just pointers to a double pointer. The define functions take no arguments and return to data.

      You should dynamically allocate whenever possible. The main reason is that is gives you control when to deallocate rather than the compiler. When you start passing pointers to allocated memory around your program, the nly safe time to deallocate is if you know there are no other copies of that address anywhere in the program. The compiler does not keep track of this so you can have a deallocation that invalidates your other copies of that address and that causes a crash.

      Overhead is not an issue at today's processor speeds.

      Comment

      • emaghero
        New Member
        • Oct 2006
        • 85

        #4
        The matrices A, B are dynamically allocated by creating an array of arrays.

        Code:
        int size = some_number; 
        A = new double *[size+1];
        for(int i=1;i<=size; i++){
        A[i] = new double [size+1]; 
        }
        The define functions are only there to illustrate what the algorithm is doing. I didn't want to clutter the post with unnecessary detail.

        It seems that the answer to my question depends on whether or not I am worried about overhead caused by the allocation / de-allocation process. Since I'm not worried about overhead I should be fine as long as I don't make multiple copies of the same address.

        Thanks for all the help.

        Comment

        Working...