Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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