![]() |
| | #1 |
| Registered User Join Date: Aug 2005
Posts: 180
| Proper Usage of the delete Operator 1.) Can I delete multiple variables with a single call to delete? What I mean is, can I do the following: Code: double *a, *b, *c; a = new double[10]; b = new double[10]; c = new double[10]; delete [] a, b, c; 2.) Is it ever ok to ommit the delete operator? For instance, if I have a function that created some dynamically allocated objects, and that functions is about to return, should my program end with the following: Code: delete [] a; delete [] b; delete [] c; return 0; } Code: return 0; } |
| thetinman is offline | |
| | #2 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
Quote:
From what I understand (or do not understand, heheh), the operating system may be able to reclaim undeleted resources after program execution ends. On the other hand, by following the rule I outlined, you ensure that there are no memory leaks even if no such resource reclamation is done.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||
| laserlight is online now | |
| | #3 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Quote:
a) using a smart pointer (but then the delete operator is merely hidden inside the smart pointer) or b) using garbage collections. You aren't.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law | |
| CornedBee is offline | |
| | #4 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| In some rare cases it is probably acceptable to delete global objects if you have some dependency issues and the destructor is trivial. However, in C++ there are very few times when you want to use dynamic arrays like that. A vector is probably much better, and you don't have to worry about memory management in this way. |
| Daved is offline | |
| | #5 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Either I'm misunderstanding what you mean, or this is plain wrong. What do you mean by "global objects"?
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #6 |
| Registered User Join Date: Jan 2005
Posts: 7,137
| I meant it is acceptable to not delete global objects. Although global objects is probably the wrong term. I'm talking about things like singletons whose lifetimes outlast main(). In some cases it is probably acceptable to not clean them up since the program is terminating anyway. |
| Daved is offline | |
| | #7 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| Yes, memory is reclaimed. But if the singletons hold anything that outlives a process (I believe shared memory in POSIX does, for example), they need to free that.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #8 | |
| "Why use dynamic memory?" Join Date: Aug 2006
Posts: 179
| Quote:
__________________ "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup OS: Microsoft XP Media Center IDE: Microsoft Visual Studio 2005 pro edition Current Porject: Developing a 2D card game... | |
| Hussain Hani is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C++ Operator Overloading help | Bartosz | C++ Programming | 2 | 08-17-2005 12:55 PM |
| private overloading of new & delete | Aidman | C++ Programming | 12 | 04-28-2005 01:11 PM |
| operator overloading and dynamic memory program | jlmac2001 | C++ Programming | 3 | 04-06-2003 11:51 PM |
| Finishing my "int class"... help with weird operator overloads | Trauts | C++ Programming | 3 | 12-06-2002 10:02 AM |
| int vs BIG INT | rumi_red | C++ Programming | 1 | 10-30-2001 04:15 AM |