C (108/304)

From:Alexander Niven-Jenkins
Date:13 Aug 2000 at 16:14:45
Subject:Re: memory allocation fcns

Hiya Allan,

On 13-Aug-00, you wrote:

>>> If you use C++ then new & delete are much better. They can be
>>> overloaded on a pr. class basis, and you can also add new arguments
>>> to the operators.

>> Do you have some examples of what you do here? [...]

> Exactly what do you want? Overloading, overloading for single classes
> only, overloading with extra arguments or just tips on how to improve
> memory behaviour (allocation and/or freeing speed, minimising wasted
> bytes and/or fragmentation)?

Every thing and anything :-)

At the moment I have a file which within it basically contains a long to
describe how many bytes of data follows.

Within a loop I am effectively doing...

Read length
data = new char[ length ];
Read lengh bytes into data;
// do stuff with data
delete[] data;

The problem I have is that length can be from 1 byte to the max of a long
and each length/data combination can be a different size.

I have got a newer version of the routine which only reallocates the
memory on change of length (I havn't implemented this yet)

Read length
if( length != prevlength )
{
delete[] data;
data = new char[ length ];
}
Read lengh bytes into data;
// do stuff with data

This must fragment memory etc, and I was wondering if there was a better
way...

Kind regards...

Alex



Cremlin Software - http://www.cremlinsoftware.org
Founder, lead programmer - ghandi@cremlinsoftware.org
Production, programmer - anj@cremlinsoftware.org