Topic: Exceptions and class-specific new


Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/04/08
Raw View
Patrick Smith wrote:
>
> Should a class-specific operator new be used to allocate the memory for
> the temporary copy of a thrown object?
>
[...]
> Section 15.1 of the standard says in paragraph 4:
>
>         The memory for the temporary copy of the exception being thrown
>         is allocated in unspecified way, except as noted in 3.7.3.1.
>

I think the intent of this paragraph is that implementations are allowed
to copy a thrown object to some buffer area kept by the implementation.
This may be to ensure that such copying doesn't throw exceptions. (Since
the usual allocator functions can throw exceptions.)

> 3.7.3.1 is a discussion of allocation functions, such as X::operator
> new.
> It ends with this note:
>
>         [Note: ... a global allocation function is not called ... for
>         the copy of an object thrown by a throw expression (15.1).]
>
> This leaves me with the impression that a class-specific allocation
> function should be called.

I don't think that your conclusion follows from the two quotes given
above. What if a class doesn't have its own allocation function?

Best regards,
Biju Thomas
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Patrick Smith <patsmith@pobox.com>
Date: 1999/04/07
Raw View
Should a class-specific operator new be used to allocate the memory for
the temporary copy of a thrown object?  For example, should this program
print anything?

 #include <iostream>

 struct X {
    static void* operator new(size_t n) {
       cout << "allocating" << endl;
       return ::operator new(n);
    }
 };

 void main() {
    try {
       throw X();
    }
    catch (X& x) {
    }
 }

Section 15.1 of the standard says in paragraph 4:

 The memory for the temporary copy of the exception being thrown
 is allocated in unspecified way, except as noted in 3.7.3.1.

3.7.3.1 is a discussion of allocation functions, such as X::operator
new.
It ends with this note:

 [Note: ... a global allocation function is not called ... for
 the copy of an object thrown by a throw expression (15.1).]

This leaves me with the impression that a class-specific allocation
function should be called.

On the other hand, the above program produces no output after being
compiled with egcs 1.1.2.
--
patsmith@pobox.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]