Preallocating memory with C++ in realtime environment
- by Elazar Leibovich
I'm having a function which gets an input buffer of n bytes, and needs an auxillary buffer of n bytes in order to process the given input buffer.
(I know vector is allocating memory at runtime, let's say that I'm using a vector which uses static preallocated memory. Imagine this is NOT an STL vector.)
The usual approach is
void processData(vector<T> &vec) {
vector<T> &aux = new vector<T>(vec.size()); //dynamically allocate memory
// process data
}
//usage:
processData(v)
Since I'm working in a real time environment, I wish to preallocate all the memory I'll ever need in advance.
The buffer is allocated only once at startup. I want that whenever I'm allocating a vector, I'll automatically allocate auxillary buffer for my processData function.
I can do something similar with a template function
static void _processData(vector<T> &vec,vector<T> &aux) {
// process data
}
template<size_t sz>
void processData(vector<T> &vec) {
static aux_buffer[sz];
vector aux(vec.size(),aux_buffer); // use aux_buffer for the vector
_processData(vec,aux);
}
// usage:
processData<V_MAX_SIZE>(v);
However working alot with templates is not much fun (now let's recompile everything since I changed a comment!), and it forces me to do some bookkeeping whenever I use this function.
Are there any nicer designs around this problem?