4 ms·
> This is like saying... be sure to never copy more bytes than the buffer capacity. Easier said than done. A solved problem except when people insist on using
by DRMacIver 14y ago
> This is like saying... be sure to never copy more bytes than the buffer capacity. Easier said than done.
A solved problem except when people insist on using approaches where it's not a solved problem? I couldn't agree more! :-)
firstly, the update example is a bait and switch. Partially updating an array is not inconsistent. It may be wrong depending on what the contract of the function says but that's but the same as inconsistent.
Indeed the example that prompted this article back when I wrote it many moons ago was a case where you wouldn't want the state restored
> In general, however, this is not sufficient since update may also have modified global variables and other objects on the heap
"Doctor! It hurts when I do this!" "Well don't do that then"
It's hard to write any correct code when you're mutating globals and arguments all over the place. Exceptions can certainly make this worse.
In general if you're not using globals or mutating your arguments this isn't a hard problem. If you are mutating arguments and you want to ensure a "restore all state if an exception is thrown behaviour then yes this is sometimes hard (unless your argument supports a rollback mechanism - e.g. it's a database) but I submit that a large part of why you haven't seen that is that it's a contract people don't care enough about to support