I highly doubt it.
There are a lot of questionable ways of freeing memory, for example you can use delete
on your char
array (rather than delete[]
) and it will likely work fine. I blogged in detail about this (apologies for the self-link, but it's easier than rewriting it all).
The compiler is not so much the issue as the platform. Most libraries will use the allocation methods of the underlying operating system, which means the same code could behave differently on Mac vs. Windows vs. Linux. I have seen examples of this and every single one was questionable code.
The safest approach is to always allocate and free memory using the same data type. If you are allocating char
s and returning them to other code, you may be better off providing specific allocate/deallocate methods:
SOME_STRUCT* Allocate()
{
size_t cb; // Initialised to something
return (SOME_STRUCT*)(new char[cb]);
}
void Free(SOME_STRUCT* obj)
{
delete[] (char*)obj;
}
(Overloading the new
and delete
operators may also be an option, but I have never liked doing this.)