473,378 Members | 1,321 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Repeated dynamic memory allocation

emaghero
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

Expand|Select|Wrap|Line Numbers
  1. int Nsteps, Npos;
  2.  
  3. double dz;
  4.  
  5. // arrays to hold the field values
  6. double *x_old;
  7. double *x_new;
  8.  
  9. // arrays to hold the matrices for the system A.x^{new} = B.x^{old}
  10. double **A;
  11. double **B;
  12.  
  13. // Assume x^{old} is defined by initial input
  14. define_x_initial(); 
  15.  
  16. for(int i=1; i<=Nsteps; i++){
  17.     // Define the matrices A and B
  18.     define_A(); 
  19.     define_B();
  20.     // Define x^{new} by solving the system
  21.     solve_system();
  22. }
  23.  
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.
Sep 6 '12 #1
3 1920
Rabbit
12,516 Expert Mod 8TB
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.
Sep 6 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Sep 6 '12 #3
The matrices A, B are dynamically allocated by creating an array of arrays.

Expand|Select|Wrap|Line Numbers
  1. int size = some_number; 
  2. A = new double *[size+1];
  3. for(int i=1;i<=size; i++){
  4. A[i] = new double [size+1]; 
  5. }
  6.  
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.
Sep 7 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: New | last post by:
I am new to java and was wondering does java have dynamic memory allocation like in C (malloc, calloc) Thank you
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
11
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I...
13
by: xian_hong2046 | last post by:
Hello, I think dynamic memory allocation is supposed to be used when one doesn't know in advance how much memory to allocate until run time. An example from Thinking in C++ is to dynamically...
2
by: nayannovellus | last post by:
As per my knowledge i know that when a structure is defined, no memory is assigned to its members but when a variable of that structure is declared memory is allocated and also this allocation takes...
17
by: farshid | last post by:
I have written a long program with c, and am using dynamic memory allocation. This program is supposed to be run over and over (300 times) for a long simulation. But the program stops after 120...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
19
by: smarty | last post by:
how can I find the memory allocated dynamically? is there any possibility of finding it?
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.