473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
Aug 17 '06 #1
6 2225

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

Aug 17 '06 #2
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<intinstead.
Best

Kai-Uwe Bux
Aug 17 '06 #3

"Pablo" <p.****@onet.euwrote in message news:ec**********@news.onet.pl...
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
Aug 17 '06 #4

"Howard" <al*****@hotmail.comwrote in message
news:db********************@bgtnsc04-news.ops.worldnet.att.net...
>
MyClass::RecreateArray( int size )
oops, obviously that should be:

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


Aug 17 '06 #5
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
Aug 18 '06 #6

"Pablo" <p.****@onet.euwrote in message news:ec**********@news.onet.pl...
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


Aug 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
This could possibly be a bug, but I don't understand it fully so I'm posting here first. Searching the list told me other people are having this problem too. I have created a class which...
5
by: Gandalf | last post by:
Hello (it's the newbie again). If I have a class class Foo{ public: Foo(){cout<<"Making"<<endl;} ~Foo(){cout<<"Destroying"<<endl;} }; void func(Foo x){}
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
3
by: JackC | last post by:
Hi Problem: I wish to use a pimpl to hide implementation/storage of a class (T), but I also want to hold objects of that class (T) in an std::vector<T> (or similar). T is non trivial (i.e. not...
5
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
4
by: Luke Matuszewski | last post by:
Here are some questions that i am interested about and wanted to here an explanation/discussion: 1. (general) Is the objectness in JavaScript was supported from the very first version of it (in...
0
by: bayan1 | last post by:
The following program have the following output. write the necessary code to make it works? Code : #include <iostream> using namespace std; class Point { private: int x; int y; public:
5
by: cctv.star | last post by:
I need to maintain certain data structure, shared by all instances of a class, declared as . This data structure must change whenever a new object is created or an existing object is destroyed. So...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.