Last active
August 29, 2015 14:02
-
-
Save mlabbe/22067a21e4b82bf8de99 to your computer and use it in GitHub Desktop.
delete[] undefined result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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