Re: A few basic questions about constructor & exception (in Managed C++ environment)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



First, you should really use delete [] instead of delete in this case. Second, I wouldn't call the destructor directly. I recommend that you introduce a Cleanup() function that you call from both the d'tor and the c'tor.

But if you really want to know my opinion, I simply wouldn't use native pointers at all. They're unsafe, from both the exception safety point of view, and from the programmer mistakes point of view.

I would simply use std::vector in most cases. That's a reasonable container. IF you know the number of items, you can use reserve to pre-allocate memory for it. Instead of TYPE_1_T *, I would use vector<TYPE_1_T>.

If that doesn't sound good, my second choice would be boost::scoped_array, which is a smart pointer similar to auto_ptr, but for arrays. It calls delete [] instead of delete to deallocate. The only time I like scoped_array better than vector is when I need to allocate uninitialized memory for extreme perforamance reasons. It doesn't happen in typical application code, but in low-level library code, such as image processing.

Both solutions are perfectly exception safe and you can't make a mistake with them, and you don't have to worry about throwing from the construtor.

If I really had to use native unsafe pointer, I would consider calling malloc and free directly, which don't throw.

Otherwise your code is working too, it's just cumbersome, difficult to write, maintain and read, and it's easy to make a mistake there. For example, if you later need to add a var6, you have to modify the code at many different places, and if you just miss one of them, you'll get into trouble. A good design is that requires the minimum amount of editing in order to introduce an additional variable.

This is just an advice, don't feel bad about it, your solution basically works (after changing delete to delete []).

Tom


Lloyd Dupont wrote:
class MyClass
{
TYPE_1_T *var1, *var2, *var, *var4, *var5;
inline void SetNull()
{
var1 = NULL;
var2 = NULL;
var3 = NULL;
var4 = NULL;
var5 = NULL;
}
public:
MyClass()
{
// NULL them first, so I know what to delete
SetNull();
try
{
// some (memory) error throwing init code
var1 = new TYPE_1_T[SomeNumber];
// ... alloc/new other variable
}
catch
{
// here I delete all my allocated var, what do you think?
~MyClass();
throw NotEnoughMemory ;
}
}
~MyClass()
{
if(var1)
delete var1;
if(var2)
delete var2;
if(var3)
delete var3;
if(var4)
delete var4;
if(var5)
delete var5;
// just in case, I'm not really confident about C++
// could I have ~MyClass() private?
SetNull();
}
}
.



Relevant Pages

  • Re: pointer/ref question
    ... >> the code in the function will allocate new memory to this address. ... > what ever that new value is use it as a pointer. ... My mistake was I was thinking in terms of passing an non null pointer. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Is this math test too easy?
    ... > communications glitch; one of the more laughable cartoons ... it was loaded into physical memory and, ... > Or one can interpret the character string as one of the values ... A pointer to an integer? ...
    (sci.math)
  • Re: grow list by tail, pointer example recipe -- please comment
    ... manufacturing a pointer with that address. ... the next cons cell. ... believe these lists are in consecutive memory locations. ...
    (comp.lang.lisp)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • Re: "Mastering C Pointers"....
    ... all means go ahead and dive right into the C language. ... Memory is a separate unit which just stores bits. ... A pointer at the hardware level _is an integer_. ... since loops make your logic more much ...
    (comp.lang.c)