Is the following safe?
struct K { ... }
struct A
{
A(int psize) : size(psize), foo(nullptr), bar(nullptr)
{
auto dataptr = (K*)_aligned_malloc(sizeof(K) * psize * 2, 32);
data = shared_ptr<K>(dataptr, ptr_fun(_aligned_free));
foo = data.get();
bar = data.get() + psize;
}
K* __restrict foo;
K* __restrict bar;
private:
shared_ptr<K> data;
};
Note the __restrict on foo and bar.
The goal is to have allocated memory self destruct when all object aliases have died.
{
A a(1000);
{
A o = a;
}
//a.foo is still valid
}
//a.foo is invalid
You don’t need
__restrictqualifiers here, and in fact you shouldn’t use them, because__restrictis supposed to tell the compiler that you don’t have any aliases to the same memory, but in fact you do have aliases —fooanddataare aliases to the same memory.I think the semantics of your code are fine, otherwise. Your a.foo is still valid and a.foo is invalid conditions will be true.