H
Hicham Mouline
Hello,
I have a function f that is called a large number of times
my current implementation is
bool f( iterator begin, iterator end ... )
{
const size_t n =end - begin;
....
....
boost::scoped_array<some_type> a( new some_type[n] );
...
....
return true;
}
However I tested the same implementation with a on-stack allocation
bool f( iterator begin, iterator end ... )
{
const size_t n =end - begin;
....
....
const size_t maxN = 64;
if (n>64)
{
return false;
}
some_type a[maxN ]; // or maybe boost::array< some_type, maxN > a;
...
....
return true;
}
Given the size of my problem, I am mostly in implementation 2, but sometimes
it may be over 64 and therefore would like to use dynamic allocation.
How can I not duplicate the code ? I can't see it.
some_type* a;
if (n>64){
a = new ...
}
else{
a = ???
}
regards,
is this the kind of things STL allocators do? should I template this
function with an allocator?
I have a function f that is called a large number of times
my current implementation is
bool f( iterator begin, iterator end ... )
{
const size_t n =end - begin;
....
....
boost::scoped_array<some_type> a( new some_type[n] );
...
....
return true;
}
However I tested the same implementation with a on-stack allocation
bool f( iterator begin, iterator end ... )
{
const size_t n =end - begin;
....
....
const size_t maxN = 64;
if (n>64)
{
return false;
}
some_type a[maxN ]; // or maybe boost::array< some_type, maxN > a;
...
....
return true;
}
Given the size of my problem, I am mostly in implementation 2, but sometimes
it may be over 64 and therefore would like to use dynamic allocation.
How can I not duplicate the code ? I can't see it.
some_type* a;
if (n>64){
a = new ...
}
else{
a = ???
}
regards,
is this the kind of things STL allocators do? should I template this
function with an allocator?