Topic: Memory allocation in Constructors


Author: rory@retix.retix.com (Rory Ward)
Date: 27 Sep 91 18:11:31 GMT
Raw View
If a constructor is used for allocating memory to a member of the
object & this allocation fails, how can this be signaled, since
constructors have no return type. Do you have to have something like
an 'allocErr' field that is checked after the 'new' returns.

Ta,
Rory
--
rory@retix.retix.com
--
rory@retix.retix.com




Author: jimad@microsoft.com (Jim ADCOCK)
Date: 1 Oct 91 19:22:50 GMT
Raw View
In article <RORY.91Sep27101131@retix.retix.com> rory@retix.retix.com (Rory Ward) writes:
|If a constructor is used for allocating memory to a member of the
|object & this allocation fails, how can this be signaled, since
|constructors have no return type. Do you have to have something like
|an 'allocErr' field that is checked after the 'new' returns.

No, rather one throws an exception.  This FAQ is covered in Stroustrup's
new "The C++ Programming Language 2nd edition" -- which I recommend highly.




Author: cimshop!davidm@uunet.UU.NET (David S. Masterson)
Date: 2 Oct 91 17:39:55 GMT
Raw View
>>>>> On 1 Oct 91 19:22:50 GMT, jimad@microsoft.com (Jim ADCOCK) said:

Jim> In article <RORY.91Sep27101131@retix.retix.com> rory@retix.retix.com
Jim> (Rory Ward) writes:

Rory> If a constructor is used for allocating memory to a member of the object
Rory> & this allocation fails, how can this be signaled, since constructors
Rory> have no return type. Do you have to have something like an 'allocErr'
Rory> field that is checked after the 'new' returns.

Jim> No, rather one throws an exception.  This FAQ is covered in Stroustrup's
Jim> new "The C++ Programming Language 2nd edition" -- which I recommend
Jim> highly.

I guess the question is how best to do this now without exception handling
that will readily move over to exception handling when it arrives in a C++
compiler near you?  (BTW, do *any* have it now?)
--
====================================================================
David Masterson     Consilium, Inc.
(415) 691-6311     640 Clyde Ct.
uunet!cimshop!davidm    Mtn. View, CA  94043
====================================================================
"If someone thinks they know what I said, then I didn't say it!"




Author: chris@chome.ruhr.de (Christoph Strozyk)
Date: 2 Oct 91 20:05:26 GMT
Raw View
rory@retix.retix.com (Rory Ward) writes:

>If a constructor is used for allocating memory to a member of the
>object & this allocation fails, how can this be signaled, since
>constructors have no return type. Do you have to have something like
>an 'allocErr' field that is checked after the 'new' returns.

You mean member of the class, not object. You have one class and
one or more objects of the class. The objects are all members
of a class.

If your 'new' doesn't handle allocation errors in a way you like
you can overload 'new' with your own 'new' which may include some
error handling ! But you are right, it is a desing problem in C++.

But also look into the ARM, chapter 15 (page 353) Exeption Handling.
In my ARM there is a comment the exeption handling is experimental.
But true is that it was accepted by the X3J1 commitee in Nov 90.
This is from the corrections to the ARM.
--
 Christoph Strozyk         |    chris@chome.en.open.de
 Sprockhoeveler Str. 55    |    2:241/5800.13
 5810 Witten, Germany      |    49-2302-25880 24h voice