Skip to content

Instantly share code, notes, and snippets.

@if1live
Created March 14, 2016 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save if1live/be4a84b4362103a45159 to your computer and use it in GitHub Desktop.
Save if1live/be4a84b4362103a45159 to your computer and use it in GitHub Desktop.
use after delete
#include <string>
#include <cstring>
class BrokenMemory {
public:
static BrokenMemory *Get();
public:
BrokenMemory() : data_("EPIC FAIL") {}
const char *data() const { return data_.data(); }
private:
static BrokenMemory *obj;
std::string data_;
};
BrokenMemory *BrokenMemory::obj = NULL;
BrokenMemory *BrokenMemory::Get() {
if(obj != NULL) {
delete(obj);
obj = NULL;
}
if(obj == NULL) {
obj = new BrokenMemory();
}
return obj;
}
int main()
{
BrokenMemory *before = BrokenMemory::Get();
BrokenMemory *after = BrokenMemory::Get();
printf("before\t:%s\n", before->data());
printf("after\t:%s\n", after->data());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment