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 25th, 2009, 02:54 AM
yoni162 yoni162 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 19 yoni162 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 1 m 48 sec
Reputation Power: 0
Memory & arrays - Should I vector.clear()?

Say I have a class with some vectors as private fields. In my program, all of these vectors will contain pointers created by "new" mechanism, therefore in the class destructor I'm deleting all the pointers. My question is, is there any point in clearing the vector afterwards, ie vector.clear() ? Or will that be taken care of by the Vector class destructor?

Thanks

Reply With Quote
  #2  
Old June 25th, 2009, 03:57 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 9 h 26 m
Reputation Power: 5
The vector destructor will take care of that. Instead of manually deleting all the pointers (which is good, many people forget that), you could also consider storing smart pointers in your vector. That way you do not need to clean up any memory manually.
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
  #3  
Old June 25th, 2009, 04:32 AM
yoni162 yoni162 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 19 yoni162 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 1 m 48 sec
Reputation Power: 0
Haven't thought about smart pointers..actually the part of the program that handles the pointer creation is out of my hands (it's a hw assignment, the main is already given as-is). Anyway thanks.

Reply With Quote
  #4  
Old June 25th, 2009, 06:33 PM
yoni162 yoni162 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 19 yoni162 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 1 m 48 sec
Reputation Power: 0
Another question

It's got nothing to do with the last one, it's just that I don't feel comfortable opening a new thread for each question.

So, is there a reason why not to use a const reference when writing a constructor? For example:
Code:
Class A
{
A(const std::string& dname) : name(dname) ...
}; 


Should I do it like this every time, for not copying objects for nothing (the &) and for not changing what I'm not supposed to (the constness)?

Reply With Quote
  #5  
Old June 26th, 2009, 01:41 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 9 h 26 m
Reputation Power: 5
Well you shouldn't do this every time. There are not many 'always-do-this' rules in programming (that's what makes it interesting?).

But yes, I find myself writing parameters as const reference quite often, almost always for strings. If you're only going to read a parameter then a const reference is most often the 'best' way to go.

For primitive types like int, float, and bool I do not pass const references though, but a copy. Which comes down to the same anyway: you make a copy so you're not changing the original (no need for const), and you might even be slightly faster since you do not have an extra indirection, ymmv.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Memory & arrays - Should I vector.clear()?


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




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
Stay green...Green IT