I have a function that receives an input buffer of n bytes and needs an auxiliary buffer of n bytes to process this input buffer.
(I know that a vector allocates memory at runtime, say I use a vector that uses static predefined memory. Imagine that it is NOT an STL vector.)
Usual approach
void processData(vector<T> &vec) { vector<T> &aux = new vector<T>(vec.size());
Since I work in real time, I want to preallocate all the memory that I need in advance.
The buffer is allocated only once at startup. I want that whenever I select a vector, I automatically allocate an auxiliary buffer for my processData function.
I can do something similar with a template function
static void _processData(vector<T> &vec,vector<T> &aux) {
However, working with templates is not very pleasant (now let me recompile everything, since I changed the comment!), And this forces me to do some accounting reporting whenever I use this function.
Are there any nicer designs around this problem?
c ++ memory-management real-time
Elazar leibovich
source share