Skip to content

Instantly share code, notes, and snippets.

@mortennobel
Last active March 13, 2018 14:12
Show Gist options
  • Save mortennobel/96015837f044785232c34786dba91586 to your computer and use it in GitHub Desktop.
Save mortennobel/96015837f044785232c34786dba91586 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <memory>
struct String {
std::string s;
String(std::string s)
:s(s) {
std::cout << "Constructor " << s << std::endl;
}
~String() {
std::cout << "Destructor " << s << std::endl;
}
};
struct Data {
std::shared_ptr<String> s;
};
int main() {
// initialize
Data* d = static_cast<Data *>(malloc(sizeof(Data)));
// empty the array
memset(d, 0, sizeof(Data));
// set value
d->s = std::make_shared<String>("Hello world");
// use
std::cout << d->s->s << std::endl;
// delete
d->s.reset();
free(d);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment