Cuberite Forum
auto_ptr with arrays - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Cuberite (https://forum.cuberite.org/forum-4.html)
+--- Forum: Development (https://forum.cuberite.org/forum-13.html)
+--- Thread: auto_ptr with arrays (/thread-442.html)



auto_ptr with arrays - FakeTruth - 05-21-2012

According to this http://stackoverflow.com/questions/6520844/using-auto-ptr-with-array and this http://forums.codeguru.com/showthread.php?t=406187

this
Code:
std::auto_ptr<char> ConvertedData(new char[ ExpectedSize ]);
is illegal.


Actually I've heard from other people that just using an auto_ptr is bad practice...


RE: auto_ptr with arrays - xoft - 05-21-2012

yeah, my bad, auto_ptr is bad for arrays. But I don't think it's so bad for normal objects that you need to get rid of at scope end.

I'll rewrite those bad char arrays. Later. Ish.

Good catch. This might be the actual cause of some failures on some platforms (unfortunately it works in MSVC, probably even in GCC)


RE: auto_ptr with arrays - xoft - 05-23-2012

I've replaced the auto_ptr usage for char arrays in the branch. I won't be porting that to the trunk, 'cause I think the branch is now in a much better shape than the trunk Wink

As for not using auto_ptr at all, I tend to disagree. It's much safer using it than not using anything at all, and it's safe enough in our context so that nothing else needs to be used.
The only reason we're using auto_ptr is its ability to delete data unless explicitly told not to. This coincides with our algorithms, producing data in buffers and throwing away those buffers if anything goes wrong. Since we're not using exceptions at all, exception safety is something we should be too concerned about - we know about them, alright, but we don't handle them anyway, so any exception will basically crash MCS, no point in deallocating memory before the crash Smile