C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-24-2007, 02:55 PM   #1
Registered User
 
Join Date: Aug 2005
Posts: 180
Proper Usage of the delete Operator

I have two questions about 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;
}
Or could I ommit the delete oporator and end the function merely by coding:

Code:
	return 0;
}
???
thetinman is offline   Reply With Quote
Old 04-24-2007, 03:01 PM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Can I delete multiple variables with a single call to delete?
I do not think so, since delete[] only takes one expression.

Quote:
Is it ever ok to ommit the delete operator?
The rule of thumb is: whatever you new, remember to delete; whatever you new[], remember to delete[].

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   Reply With Quote
Old 04-24-2007, 03:07 PM   #3
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
Quote:
2.) Is it ever ok to ommit the delete operator?
Yes. When you're
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   Reply With Quote
Old 04-24-2007, 04:28 PM   #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   Reply With Quote
Old 04-25-2007, 05:10 AM   #5
Cat without Hat
 
CornedBee's Avatar
 
Join Date: Apr 2003
Posts: 8,439
Quote:
Originally Posted by Daved View Post
In some rare cases it is probably acceptable to delete global objects
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   Reply With Quote
Old 04-25-2007, 12:49 PM   #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   Reply With Quote
Old 04-25-2007, 01:20 PM   #7
Cat without Hat
 
CornedBee's Avatar
 
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   Reply With Quote
Old 04-25-2007, 11:53 PM   #8
"Why use dynamic memory?"
 
Join Date: Aug 2006
Posts: 179
Quote:
Or could I ommit the delete oporator and end the function merely by coding:
Code:
return 0;
C++ doesnt have garbage collection, so dont wait for the "return" to end the whole program
__________________
"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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:33 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22