C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 2nd, 2004, 03:45 AM
Orca Orca is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 Orca User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Question regarding destructors

I'm not very experienced with C++, but I am writing a practical application for my internship job. So you can imagine how nervous I am now since it crashes everytime I run it =)

This is a very simple class for coordinate objects, consisting of an interger x and y value, as well as some basic geometry functions. My program works when I'm not initializing a lot of these objects, but when I do it crashes and Windows NT gives me a program error warning. I've traced my program and it seems to be a problem with the destructor. The program always freezes when it tries to exit the destructor for a certain object after a while.

Hence I think that I might be using too much memory from constructing all these objects. Right now my destructor is empty, since all the C++ tutorials I've found online only specify how to free memory for pointers and arrays. I'd be very glad if someone could tell me what is wrong or how to solve this problem ^_^

Here are my constructor and destructor methods for reference:

coor::coor() //default paramenters set to (0,0)
{ x= 0;
y= 0;
}

coor::coor(int new_x, int new_y)
{ x = new_x;
y = new_y;
}

coor::~coor() //destructor is empty
{
}

Thanks!

Reply With Quote
  #2  
Old June 9th, 2004, 03:24 AM
Orca Orca is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 Orca User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Solution

Though no one replied, I solved my problem a while later, and I'll post the solution here just in case someone had a similar question.
I asked this same question on another forum and got some useful answers from there too.

The main problem with the crashing was not in the destructor, but in the assignment operator overloading. My original assignment overloading code included a delete statement, which was there in the first place since I got that code from a website.

My program worked fine when I took out those delete statements. What people told me was that since I didn't have any arrays or pointers in my class, I didn't have to specify a destructor for them or delete them in my assignment overloading. The compiler would take care of these built in types such as int, char etc. The delete command is only used for pointers and arrays.

Reply With Quote
  #3  
Old July 1st, 2004, 10:48 PM
Monkey Junk Monkey Junk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Monkey Junk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
When you are using scalar data types, the default destructor will clean house for you.

Maybe a matter of preference, but why don't you place your default values in the parameter listing within your specification file for your class. When the object is called with no arguments, these values are automatically assigned.

coor (int = 0, int = 0);

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Question regarding destructors


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT