[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Can't get custom allocation function to work
- From: "kathrin_69@..." <kathrin_69@...>
- Date: Tue, 24 Mar 2009 15:59:24 +0100
Ralph Hempel wrote:
I'm trying to replace the Lua default l_alloc function with my own
one. My aim is to have all memory Lua allocates inside a single array
of pre-allocated memory.
void* alloc_impl_manual( void* ptr, size_t osize, size_t nsize )
std::vector<unsigned char>* mem = new std::vector<unsigned
//yes, it will leak. don't care for now
return &(*mem); //returned memory position should be aligned
Ummm, besides the leak, should you not be returning:
return( mem )
since mem is already a pointer to the memory you allocated?
And this expression &(*mem) is very strange. I parse that as:
int* c_array = &v; //not just &v !
printf( "%d%d", c_array, c_array ); //"42"
The expression gives the position of the continuous memory regions hold
by std::vector. mem would just be the this-pointer of the vector object
and therefore the wrong thing.
This is an stl feature to get backward compatibility to c-style arrays
as shown in the little example above.
(the adress of ( (the contents of mem) ???? )
my compiler warns that the subscripted value is neither an array nor a
John had the right solution. I forgot to move the old mem to the new
range in case of a re-allocation. Was my first allocator :)
Thank you all!