Last active
August 29, 2015 13:56
-
-
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?
This file contains hidden or 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 <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