From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Re: Allocators and exceptions
Date: Wed, 12 Sep 2007 05:36:54 -0700
Date: 2007-09-12T05:36:54-07:00 [thread overview]
Message-ID: <1189600614.629447.86470@k79g2000hse.googlegroups.com> (raw)
In-Reply-To: <1189551376.12652.36.camel@kartoffel>
On 12 Wrz, 00:56, Georg Bauhaus <rm.tsoh+bauh...@maps.futureapps.de>
wrote:
> > But... did I mention that C++ handles this issue correctly? ;-)
> > And no, it does not have any super-capable access types. A little bit
> > smarter allocator is enough.
>
> C++ doesn't seem to handle deallocation etc. of sibling components
> either, so I'm not sure I understand.
It does. In case of exception the already constructed components are
rolled back. This works for components of array as well.
> Should the following be excluded from consideration? (The program
> quickly claims all available memory, as I would have expected; any
> pointers to what should I read to learn how this can be simply
> prevented in C++?)
>
> class T {
> int c;
> int *x;
> public:
> T(int init) {
> x = new int[1024];
> if (init < 1) { throw "Constraint_Error"; }
Yes, this is very bad coding. We call it "C with classes" to clearly
distinguish it from C++.
Try this:
class T
{
int c;
vector<int> x;
public:
T(int init)
{
x.resize(1024);
if (init < 1) { throw "Constraint_Error"; }
}
// note: no destructor needed
};
Alternatively:
class T
{
int c;
scoped_array<int> x;
public:
T(int init)
{
x = new int[1024];
if (...
...
And so on.
C++ will roll-back all already created components by calling their
destructors. In your initial ("C with classes") example, there were
two components:
int c;
int *x;
Both will be properly rolled back, which here means do nothing - these
are fundamental types.
Use smarter types to get smarter rollback.
Use C++ instead of "C with classes" and the difference will be
significant.
Note that with smarter types the code is actually shorter (no explicit
destructor needed). Less coding, less bugs, ... ;-)
--
Maciej Sobczak
http://www.msobczak.com/
next prev parent reply other threads:[~2007-09-12 12:36 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-09-09 7:40 Allocators and exceptions Maciej Sobczak
2007-09-09 12:17 ` anon
2007-09-09 20:31 ` Maciej Sobczak
2007-09-09 22:43 ` Simon Wright
2007-09-10 12:10 ` Maciej Sobczak
2007-09-10 19:08 ` Simon Wright
2007-09-10 2:56 ` anon
2007-09-10 12:42 ` Dmitry A. Kazakov
2007-09-10 21:48 ` Maciej Sobczak
2007-09-11 9:16 ` Dmitry A. Kazakov
2007-09-11 9:19 ` Maciej Sobczak
2007-09-11 12:27 ` Dmitry A. Kazakov
2007-09-11 19:07 ` Maciej Sobczak
2007-09-11 22:56 ` Georg Bauhaus
2007-09-12 12:36 ` Maciej Sobczak [this message]
2007-09-12 22:19 ` Randy Brukardt
2007-09-12 9:32 ` Dmitry A. Kazakov
2007-09-12 12:42 ` Maciej Sobczak
2007-09-12 15:25 ` Dmitry A. Kazakov
2007-09-12 12:29 ` Stephen Leake
2007-09-12 12:46 ` Maciej Sobczak
2007-09-12 20:53 ` Simon Wright
2007-09-12 22:32 ` Randy Brukardt
2007-09-12 23:43 ` Simon Wright
2007-09-13 3:42 ` Randy Brukardt
2007-09-13 3:36 ` Randy Brukardt
2007-09-13 9:43 ` Maciej Sobczak
2007-09-12 22:25 ` Randy Brukardt
2007-09-13 11:51 ` Stephen Leake
2007-09-12 14:14 ` Markus E L
2007-09-10 10:37 ` Allocators and exceptions => Read Me First anon
2007-09-10 12:16 ` Maciej Sobczak
2007-09-10 22:10 ` Allocators and exceptions => Trying Again anon
2007-09-10 23:15 ` Markus E L
2007-09-10 15:44 ` Allocators and exceptions Adam Beneschan
2007-09-10 21:58 ` Maciej Sobczak
2007-09-10 22:07 ` Jeffrey R. Carter
2007-09-11 9:14 ` Dmitry A. Kazakov
2007-09-11 9:23 ` Maciej Sobczak
2007-09-11 2:36 ` Randy Brukardt
2007-09-11 15:33 ` Adam Beneschan
2007-09-11 19:21 ` Maciej Sobczak
2007-09-11 21:56 ` Adam Beneschan
2007-09-12 0:34 ` Jeffrey R. Carter
2007-09-12 12:13 ` Maciej Sobczak
2007-09-12 16:34 ` Jeffrey R. Carter
2007-09-12 23:50 ` Jeffrey R. Carter
2007-09-12 12:22 ` Maciej Sobczak
2007-09-12 14:11 ` Markus E L
2007-09-12 16:08 ` Adam Beneschan
2007-09-12 20:35 ` Dmitry A. Kazakov
2007-09-12 21:01 ` Adam Beneschan
2007-09-12 22:45 ` Randy Brukardt
2007-09-13 7:48 ` Dmitry A. Kazakov
2007-09-12 3:08 ` Allocators and exceptions -- Debugging says memory leak! anon
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox