Hello everyone!
I was playing around whith the memory allocators provided by the library in order to understand better how to work with them. This is what I did:
class Number {
private:
int n;
public:
Number(): n(9){};
Number( const int& i): n(i){};
const int Num(void) const { return n; }
void SetNum(int i) { n = i; }
};
static Number* AllocateNumber( const int n ) {
Numero* num = (Number*)tbb::tbb_allocator().allocate(sizeof(Number)+n);
num->SetNum( n );
return num;
};
static void DeallocateNumber( Number* n ) {
tbb::tbb_allocator().deallocate((int*)n,n->Num());
};
int main(){
Number* one = AllocateNumber( 1 );
// more things
DeallocateNumber( one );
}This works fine(I think) but then I tried this other thing:
class Real {
private:
int n;
double r;
public:
Real(): n(9),r(500.50){};
Real( const int& i, const double& j ): n(i),r(j){};
const int Num(void) const { return n; }
const double Real(void) const { return r; }
void SetNum(int i) { n = i; }
void SetReal(double j) {r = j; }
};
static Real* AllocateReal( const int n, const double r ) {
Real* num = (Real*) tbb::tbb_allocator().allocate(sizeof(Real)+n+r);
num->SetNum(n);
num->SetReal;
return num;
}This fails since I'm passing two arguments to the allocator template. So my question is, how can I allocate a Real object with two data members( an int and a double)? Is something like the first AllocateNumber or a workaround is needed?
Thanks!



