Skip to content

Instantly share code, notes, and snippets.

@mlabbe
Last active August 29, 2015 14:02
Show Gist options
  • Save mlabbe/22067a21e4b82bf8de99 to your computer and use it in GitHub Desktop.
Save mlabbe/22067a21e4b82bf8de99 to your computer and use it in GitHub Desktop.
delete[] undefined result
#include<cstdio>
using namespace std;
class Base
{
public:
virtual ~Base() {}
};
#define IMPLEMENT_DERIVED_FREE(CLASS_NAME) \
static void FreePool( Base *basePtr ) \
{ \
CLASS_NAME *derivedPtr = reinterpret_cast<CLASS_NAME*>(basePtr); \
delete [] derivedPtr; \
} \
class Derived : public Base
{
public:
IMPLEMENT_DERIVED_FREE(Derived);
float bar; // This pads it, and it crashes. Undefined result either way.
};
int main( void )
{
Base *ptr = new Derived[10]; // BUG BUG
delete []ptr;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment