Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string foo()
- {
- char *p = get_string();
- string str( p );
- free( p );
- return str;
- }
- string
- foo()
- {
- shared_ptr<char> p(get_string(), &free);
- string str(p.get());
- return str;
- }
- string foo()
- {
- const char* p = get_string();
- const Cleanup cleanup(p);
- const std::string str(p != NULL ? p : "");
- return str;
- }
- struct Cleanup {
- void* toFree;
- Cleanup(void* toFree) : toFree(toFree) {}
- ~Cleanup() { free(toFree); }
- private:
- Cleanup(Cleanup&);
- void operator=(Cleanup&);
- };
- string foo()
- {
- char *p = get_string();
- ScopeGuard sg = MakeGuard(&free, p);
- string str( p );
- return str;
- }
Add Comment
Please, Sign In to add comment