Skip to content

Instantly share code, notes, and snippets.

@prat0318
Last active August 29, 2015 13:56
Show Gist options
  • Save prat0318/8893435 to your computer and use it in GitHub Desktop.
Save prat0318/8893435 to your computer and use it in GitHub Desktop.
operator delete(ptr) doesn't work when the ptr has been initialised with new as operator syntax. WHY?
#include <iostream>
#include <cstdint>
using namespace std;
int main() {
cout<<"Hello World! \n";
//For invalid pointer, uncomment the below line...
//string* s = new string[10];
//And comment the following two lines...
string* s = (string *) operator new(10 * sizeof(string));
for (uint32_t i=0; i < 10; ++i) new (s + i) string {};
for (uint32_t i=0; i < 10; ++i) {
s[i].~string();
}
// XXX Why is this failing and giving invalid pointer?
operator delete(s);
//even delete[] doesn't work.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment