And peformance-wise, are there some better ways to do that?
for example, create a class/struct called arraydata, it allocates some aligned memory for use (though a pointer provided by .dataPtr):
class arraydata//to allocate some memory,
//and return a pointer to that block of memory
{
void *dataPtrV;
public:
double *dataPtr;
arraydata(int a, int b)
{
dataPtrV=_aligned_malloc(a*b*sizeof(double),32);
dataPtr=(double *)dataPtrV;
}
~arraydata()
{
_aligned_free(dataPtrV);
dataPtrV=NULL;
dataPtr=NULL;
}
};
Then call it by:
arraydata X(30,20);
Yes, that would be considered RAII – the resource is acquired in the constructor and released in the destructor.
I’m not sure why you’re storing both a
void*and adouble*though – only adouble*should suffice.Also, be extremely careful when copying your class as that will easily lead to leaks and freeing already freed data.
Anyway, this can also be done using
std::unique_ptrwhich is more idiomatic and without the downfalls of your class:Here’s your class without
void*: