I'm trying to (and have solved) a problem with 16 byte alignment issues with a class that contains SSE optimised members. But what is bugging me is a large portion of the examples I have found online contain a line of code that to me seems totally redundant yet is repeated in many places.
public:
void* operator new (size_t size)throw (std::bad_alloc)
{
void * p = _aligned_malloc(size, 16);
if (p == 0) throw std::bad_alloc();
return p;
}
void operator delete (void *p)
{
Camera* pC = static_cast<Camera*>(p);
_aligned_free(p);
}
The line in question is
Camera* pC = static_cast<Camera*>(p);
As pC is never referenced and goes out of scope at the end of the function, what is the point of doing it? I have tried taking the line out and it appears to make no difference at all yet that line appears in lots of examples! Am I missing something really obvious or has an anomalous line of code been copied from example to example blindly and become prevalent across a lot of "tutorials"?
Camera
then certainly it has been copied from tutorial to tutorial. Blindly or otherwise. – ProgresspC
points has already been destructed, and therefore won't look quite as they might expect. – Progressoperator new
andoperator delete
is almost certainly not a good idea for this. Also, note that youroperator new
is broken since it should call allnew_handler
s in turn before throwing. /EDIT: forget the first part of the comment, I didn’t notice that this was inside a class. The second part of the comment still holds. – Majeoperator new
is dodgy anyway, so it might even turn out that it's best to find another tutorial. – Progressvoid*
like the uniformity of starting every function by casting it to the real type, hoping it'll make it easier to quickly insert meaningful operations like a line of trace. – Hogennullptr
(and be no-throw), or throw an exception (catchable viastd::bad_alloc
) in case of failure. – Airflow