Tech Support > Computers & Technology > Programming > A question about dynamic memory allocation
A question about dynamic memory allocation
Posted by Sean on June 27th, 2008



I want to write a function which will be called repeatedly for >
10000000 times (it is used in a simulation program) This function
needs a workspace which can be determined by the parameters of this
function.
The size of the workspace that is needed for a specific simulation is
usually fixed or upper bounded.
Should I use dynamic memory allocation within this function to
allocate the workspace memory
each time this function is being called, or allocate a memory block as
the workspace before this function is being repeatedly called?

I think the second method will be more efficient but make the function
call more complicated. How much gain I can have by using the second
method? I've heard that if you use malloc-free or new-delete
repeatedly, than the memory will become discontinuous, which makes the
program run less efficiently, is this true?

Posted by Sean on June 27th, 2008


On Jun 27, 2:11 am, Sean <guo.xiaoy...@gmail.com> wrote:
Allocate memory and free it each time the function is called
void function(int n)
{
double* workspace = new double[n];

....................
....................
delete[] workspace;
}


use pre-assigned workspace

void function(int n, double* workspace)
{
............................
}

Posted by Ben Bacarisse on June 27th, 2008


Sean <guo.xiaoyong@gmail.com> writes:

Or, much more C++ish:

#include <boost/shared_ptr.hpp>
#include <vector>

void function(int n)
{
static boost::shared_ptr<std::vector<double> >
workspace(new std::vector<double>);
...
}

no need to delete at all. You get a pointer to a re-sizable vector
that gets deleted at program termination. You can, of course,
pre-allocate the space in the vector to save on re-sizing costs.

--
Ben.

Posted by Jens Thoms Toerring on June 27th, 2008


Sean <guo.xiaoyong@gmail.com> wrote:
How much memory do you pre-allocate for 'workspace' to point to
if the amount required seems to depend on 'n' (at least the
first example makes it look like that)? Do you know about an
upper limit? Is this upper limit something you can only compute
at run-time or can it be made a compile-time constant? In the
later case you could also do (in C, my C++ isn't good enough)

void function(int n, double* workspace)
{
static double * workspace = malloc( UPPER_LIMIT );
............................
}

If you want to clean up afterwards and you have a value of 'n'
that never can appear during the calculations you could call
free() when this special value is passed to the function.

And then there are still the good old, much hated global
variables. 'workspace' might be a candidate for that.

But if you want to know the impact of calling malloc() and free()
(or new and delete) that many times you simply have to measure.
Nothing else can give you solid numbers, especially not rumors
you heard on the internet about the bad/good performance of me-
mory allocation functions/operators.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Posted by Steve O'Hara-Smith on June 27th, 2008


On 27 Jun 2008 11:58:53 GMT
jt@toerring.de (Jens Thoms Toerring) wrote:

Even if it's not easy to determine the upper bound you can do
something like this (in C)

void function (...)
{
static double *workspace = NULL;
static size_t workspace_size;
....
if (NULL == workspace) {
workspace = malloc (size_needed);
workspace_size = size_needed;
}
if (size_needed > workspace_size) {
workspace = realloc (workspace, size_needed);
workspace_size = size_needed;
}
....
}

Add error handling of course.

Indeed - although it is certain to be greater than not calling them.

--
C:>WIN | Directable Mirror Arrays
The computer obeys and wins. | A better way to focus the sun
You lose and Bill collects. | licences available see
| http://www.sohara.org/


Similar Posts