P
Pablo
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
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