Help with destruction of an object created with the new operator- destructors

Discussion in 'C++' started by Pablo, Aug 17, 2006.

  1. Pablo

    Pablo Guest

    Hello,
    I am writing a windows application using C++ and BorlandBuilder 6
    compiler. It is an event driven program and I need to create objects of
    some classes written by me. One of the classes contains a pointer to int
    values as a filed. In the definition (implementation) of constructor I
    use this pointer to create table of int values with the new operator.
    The number of elements of the table is provided by the user during
    execution of the program (dynamic memory allocation).
    After generating the objects and the tables, the results are displayed
    in another form (another unit i.e. cpp file is created).

    After seeing the results, the user may want to quit the program or
    generate another object with the same name but the number of elements
    in the table can be different (less or more elements) and again see the
    results.
    My question is:
    when I should destroy the object and the table and release the memory
    from the free store?

    I know that once the memory is allocated with the new operator it has to
    be released with the delete. I think that delete should be used in
    definition of the destructor. I think that I should release allocated
    memory before exiting the program. In my program the exiting is done by
    pressing button and the code for Button1Click is simply execution of the
    method Close() for the main form. So before exiting the code for
    destructors is written there. The code obviously consists of delete
    operator for releasing memory reserved for tables.

    But if the user wants to generate the object again, I should release
    the memory and destroy the object in order to create it again. Should I
    call the destructors by writing the code i.e.

    ClassName::~ClassName();

    before starting the generation of the object again?

    Regards
    Pawel
     
    Pablo, Aug 17, 2006
    #1
    1. Advertising

  2. Pablo

    Kai-Uwe Bux Guest

    Re: Help with destruction of an object created with the new operator - destructors

    Pablo wrote:

    > Hello,
    > I am writing a windows application using C++ and BorlandBuilder 6
    > compiler. It is an event driven program and I need to create objects of
    > some classes written by me. One of the classes contains a pointer to int
    > values as a filed. In the definition (implementation) of constructor I
    > use this pointer to create table of int values with the new operator.
    > The number of elements of the table is provided by the user during
    > execution of the program (dynamic memory allocation).
    > After generating the objects and the tables, the results are displayed
    > in another form (another unit i.e. cpp file is created).
    >
    > After seeing the results, the user may want to quit the program or
    > generate another object with the same name but the number of elements
    > in the table can be different (less or more elements) and again see the
    > results.
    > My question is:
    > when I should destroy the object and the table and release the memory
    > from the free store?
    >
    > I know that once the memory is allocated with the new operator it has to
    > be released with the delete.


    You said you allocate a table, then you want to use new[] and delete[]
    accordingly. (However, see the final paragraph.)

    > I think that delete should be used in
    > definition of the destructor.


    That is the standard way of doing it: new in constructors match a delete in
    the destructor. This way, every object cleans up its own mess.

    > I think that I should release allocated
    > memory before exiting the program. In my program the exiting is done by
    > pressing button and the code for Button1Click is simply execution of the
    > method Close() for the main form. So before exiting the code for
    > destructors is written there. The code obviously consists of delete
    > operator for releasing memory reserved for tables.


    I don't follow this: what does this Close() method do? Does it call exit()
    or halt()? How does it terminate the program? Since Close() is not a
    standard method, I have no way to tell whether object will be properly
    destructed. If they are (and they should), then putting delete[] within the
    destructor would be sufficient.

    >
    > But if the user wants to generate the object again,


    That would be impossible, you cannot resurrect objects. Only a different
    object can be created replacing the dead body of the old one.

    > I should release
    > the memory and destroy the object in order to create it again. Should I
    > call the destructors by writing the code i.e.
    >
    > ClassName::~ClassName();
    >
    > before starting the generation of the object again?


    No. The language does lifetime management for you in automatic and temporary
    objects, and you do lifetime management for objects created via new and
    new[] by calling delete and delete[]. Explicit destructor calls are rarely
    what you want (I only use them when I write my own allocators replacing
    std::allocator). In your case, it seems, undefined behavior would very
    likely be the result of explictly calling the destructor.


    Finally: you should not use int* for any of this anyway. The management of
    pointers (especially arrays) is a little tricky when it comes to exception
    safety. Just use std::vector<int> instead.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 17, 2006
    #2
    1. Advertising

  3. Pablo

    peter koch Guest

    Re: Help with destruction of an object created with the new operator - destructors

    Pablo skrev:

    > Hello,
    > I am writing a windows application using C++ and BorlandBuilder 6
    > compiler. It is an event driven program and I need to create objects of
    > some classes written by me.
    > One of the classes contains a pointer to int
    > values as a filed. In the definition (implementation) of constructor I
    > use this pointer to create table of int values with the new operator.

    This sounds like you should use std::vector
    [snip]
    > After seeing the results, the user may want to quit the program or
    > generate another object with the same name but the number of elements
    > in the table can be different (less or more elements) and again see the
    > results.
    > My question is:
    > when I should destroy the object and the table and release the memory
    > from the free store?


    You should most likely create a new element for each iteration. If the
    only purpose is to save a little time creating and destroying objects,
    then you definitely should create a new object every time. First
    because the most important aspect of your code is clarity (e.g. it
    should be easy to read by a fellow programmer), secondly because it
    very well might be faster this way (or not measurably slower).

    >
    > I know that once the memory is allocated with the new operator it has to
    > be released with the delete. I think that delete should be used in
    > definition of the destructor.

    delete calls the destructor.
    > I think that I should release allocated
    > memory before exiting the program. In my program the exiting is done by
    > pressing button and the code for Button1Click is simply execution of the
    > method Close() for the main form. So before exiting the code for
    > destructors is written there. The code obviously consists of delete
    > operator for releasing memory reserved for tables.


    Again, use a std::vector and you will not have any dynamic memory in
    your class.
    >
    > But if the user wants to generate the object again, I should release
    > the memory and destroy the object in order to create it again. Should I
    > call the destructors by writing the code i.e.
    >
    > ClassName::~ClassName();
    >
    > before starting the generation of the object again?
    >

    (The syntax is object->~ClassName(); )
    No - destructors should almost never be called directly. If you have
    newed your object, delete object is enough.

    /Peter
     
    peter koch, Aug 17, 2006
    #3
  4. Pablo

    Howard Guest

    Re: Help with destruction of an object created with the new operator - destructors

    "Pablo" <> wrote in message news:ec1bgq$v16$...
    > Hello,
    > I am writing a windows application using C++ and BorlandBuilder 6
    > compiler. It is an event driven program and I need to create objects of
    > some classes written by me. One of the classes contains a pointer to int
    > values as a filed. In the definition (implementation) of constructor I use
    > this pointer to create table of int values with the new operator. The
    > number of elements of the table is provided by the user during execution
    > of the program (dynamic memory allocation).
    > After generating the objects and the tables, the results are displayed in
    > another form (another unit i.e. cpp file is created).
    >
    > After seeing the results, the user may want to quit the program or
    > generate another object with the same name but the number of elements
    > in the table can be different (less or more elements) and again see the
    > results.
    > My question is:
    > when I should destroy the object and the table and release the memory from
    > the free store?
    >
    > I know that once the memory is allocated with the new operator it has to
    > be released with the delete. I think that delete should be used in
    > definition of the destructor. I think that I should release allocated
    > memory before exiting the program. In my program the exiting is done by
    > pressing button and the code for Button1Click is simply execution of the
    > method Close() for the main form. So before exiting the code for
    > destructors is written there. The code obviously consists of delete
    > operator for releasing memory reserved for tables.
    >
    > But if the user wants to generate the object again, I should release
    > the memory and destroy the object in order to create it again. Should I
    > call the destructors by writing the code i.e.
    >
    > ClassName::~ClassName();
    >
    > before starting the generation of the object again?
    >


    If you use new[] to create an array of int's in the constructor, then your
    should use delete[] to delete them in the destructor. (You'll probably also
    want to add a copy-constructor and an assignment operator. Read up on the
    "Rule of Three". A google search will find plenty of info on that.)

    You have two options (at least) for handling creation of a new array.

    When you want a new array, you could delete your instance of the class which
    created the array, and then create a new instance of that class. When you
    delete the old instance, its destructor will delete[] the array for you
    (assuming you followed my earlier instruction). Then when you create the
    new instance of the class, its constructor will allocate a new array using
    new[] (according to your comments, at least).

    Alternatively, you can add a function to that class which lets you delete
    the old array and allocate a new one. Something like this:

    MyClass::RecreateArray( int size )
    {
    delete [] myIntArray;
    myIntArray = NULL;
    myIntArray = new int[size];
    }

    (I set the pointer to NULL just in case the new[] fails, so that when the
    destructor is called, the delete[] call inside the destructor won't attempt
    to delete the array a second time.)

    -Howard
     
    Howard, Aug 17, 2006
    #4
  5. Pablo

    Howard Guest

    Re: Help with destruction of an object created with the new operator - destructors

    "Howard" <> wrote in message
    news:db3Fg.260345$...

    >
    > MyClass::RecreateArray( int size )


    oops, obviously that should be:

    void MyClass::RecreateArray( int size )

    > {
    > delete [] myIntArray;
    > myIntArray = NULL;
    > myIntArray = new int[size];
    > }
    >
     
    Howard, Aug 17, 2006
    #5
  6. Pablo

    Pablo Guest

    Howard wrote:

    >>when I should destroy the object and the table and release the memory from
    >>the free store?

    >
    > If you use new[] to create an array of int's in the constructor, then your
    > should use delete[] to delete them in the destructor. (You'll probably also
    > want to add a copy-constructor and an assignment operator. Read up on the
    > "Rule of Three". A google search will find plenty of info on that.)

    Thank you for the hint, I'll definitely do that. I'd like to thank all
    of you who helped me understand problem of creation and destruction of
    instances however, I have some more questions...

    > You have two options (at least) for handling creation of a new array.
    >
    > When you want a new array, you could delete your instance of the class which
    > created the array, and then create a new instance of that class. When you
    > delete the old instance, its destructor will delete[] the array for you
    > (assuming you followed my earlier instruction). Then when you create the
    > new instance of the class, its constructor will allocate a new array using
    > new[] (according to your comments, at least).
    >

    But what would happen at the beginning, when the program runs and
    creates the instance (and the array) for the first time?
    Only the pointer is declared but the new didn't do anything yet. So the
    pointer is pointing to NULL and the destruction will not take place - is
    that correct way of thinking?

    Regards
    Pablo
     
    Pablo, Aug 18, 2006
    #6
  7. Pablo

    Howard Guest

    Re: Help with destruction of an object created with the new operator - destructors

    "Pablo" <> wrote in message news:ec3s4u$4ic$...
    > Howard wrote:
    >
    >>>when I should destroy the object and the table and release the memory
    >>>from the free store?

    >>
    >> If you use new[] to create an array of int's in the constructor, then
    >> your should use delete[] to delete them in the destructor. (You'll
    >> probably also want to add a copy-constructor and an assignment operator.
    >> Read up on the "Rule of Three". A google search will find plenty of info
    >> on that.)

    > Thank you for the hint, I'll definitely do that. I'd like to thank all of
    > you who helped me understand problem of creation and destruction of
    > instances however, I have some more questions...
    >
    >> You have two options (at least) for handling creation of a new array.
    >>
    >> When you want a new array, you could delete your instance of the class
    >> which created the array, and then create a new instance of that class.
    >> When you delete the old instance, its destructor will delete[] the array
    >> for you (assuming you followed my earlier instruction). Then when you
    >> create the new instance of the class, its constructor will allocate a new
    >> array using new[] (according to your comments, at least).
    >>

    > But what would happen at the beginning, when the program runs and creates
    > the instance (and the array) for the first time?
    > Only the pointer is declared but the new didn't do anything yet. So the
    > pointer is pointing to NULL and the destruction will not take place - is
    > that correct way of thinking?
    >


    If the pointer is set to NULL in the constructor, any call to delete[] on
    that NULL pointer will do nothing. It's perfectly safe.

    -Howard
     
    Howard, Aug 18, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    318
  2. Fei Liu
    Replies:
    3
    Views:
    548
    Roland Pibinger
    Apr 25, 2007
  3. רמי

    Destructors And Operator Overloading

    רמי, Apr 10, 2008, in forum: C++
    Replies:
    9
    Views:
    400
    רמי
    Apr 10, 2008
  4. xmllmx
    Replies:
    6
    Views:
    418
    xmllmx
    Feb 3, 2010
  5. Andrew Torda
    Replies:
    16
    Views:
    182
Loading...

Share This Page