Re: different behavour of printf() on dos and linux
kenneth wrote:
On Oct 5, 10:41 pm, Hyman Rosen <hyro...@mail.com> wrote:
How about that in the following code
#include <memory>
typedef std::auto_ptr<int> api;
void f(api, api) { }
int main() { f(api(new int(1)), api(new int(2))); }
it is not guaranteed that all successfully allocated memory
is freed when one of the allocations throws an exception?
Yes, it is. At least it should be, despite some buggy implementations.
No, it's not.
What the standard says is that, given the code above, you cannot
assert which comes before which, new int(1) or new int(2).
That's true, but it doesn't get at the underlying issue here, which
isn't about order of evaluation. The potential problem is that the
compiler might do both new operations before calling the api
constructors; if the second new throws an exception, the first memory
block will be leaked. There are several bludgeons that could eliminate
this problem; one of them is requiring a particular order of evaluation;
another, less drastic, is introducing sequence points between
evaluations of arguments to functions.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of
"The Standard C++ Library Extensions: a Tutorial and Reference"
(www.petebecker.com/tr1book)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]