From: Kim G. <kim...@gm...> - 2011-02-01 20:10:07
|
Erik, Dean On Tue, Feb 1, 2011 at 20:24, Nelson, Erik - 2 <eri...@ba...> wrote: > >> For data that's already in memory I get the utility of being able to >> refer to the bytes directly. Unfortunately making the library >> deallocate memory that the user allocated is, quite bluntly, bad form >> and error prone. >> >> Makes sense? > > I'd say that passing ownership of an object to a library is neither bad form nor error prone. Sutter has a good writeup of > using auto_ptr effectively in 'Exceptional C++' that seems applicable. Transferring object ownership with auto_ptr is > safe, efficient, and idiomatic. I agree with this. Using auto_ptr to transfer ownership seems like a no-brainer. However, it doesn't really solve the transfer-data-without-copy problem, at least not entirely. What if I have a char buffer I got out of some C API, and I'd like to transfer that; char buffer[1024]; unsigned long bufsize = sizeof(buffer) / sizeof(*buffer); GetUserNameA(buffer, &bufsize); // Now what? I think in the cases where your data comes from somewhere out of your control and it's not packed in an auto_ptr, you'll be just as screwed as with the current std::string -- in order to get it transferable you would have to reallocate it into an auto_ptr, and that might even involve packing it into an auto_ptr< vector<T> >, which feels pretty raw. I don't have a good idea for how to solve the lifetime issues of the payload, but an auto_ptr probably won't help (much) here. Cheers, - Kim |