Warpin
Warpin

Reputation: 7043

boost::shared_array and aligned memory allocation

In Visual C++, I'm trying to dynamically allocate some memory which is 16-byte aligned so I can use SSE2 functions that require memory alignment. Right now this is how I allocate the memory:

boost::shared_array aData(new unsigned char[GetSomeSizeToAllocate()]);

I know I can use _aligned_malloc to allocate aligned memory, but will that cause a problem with boost when it tries to free my memory? This is the code boost uses to free the memory:

template inline void checked_array_delete(T * x)
{
    typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
    (void) sizeof(type_must_be_complete);
    delete [] x;
}

Memory free'd by delete must be allocated with new, right? Any tip on how I can get around this?

Upvotes: 0

Views: 1087

Answers (1)

UncleBens
UncleBens

Reputation: 41351

boost::shared_array has a constructor that takes a deleter as a second argument to be used instead of default delete[].

This means you might be able to pass the address of a suitable deallocation function just like that.

boost::shared_array<X> array(allocate_x(100), &deallocate_x);  

References: Boost.SharedArray

Upvotes: 1

Related Questions