Proper Usage of the delete Operator

This is a discussion on Proper Usage of the delete Operator within the C++ Programming forums, part of the General Programming Boards category; I have two questions about the delete operator. 1.) Can I delete multiple variables with a single call to delete? ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    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;
    }
    ???

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,440
    Can I delete multiple variables with a single call to delete?
    I do not think so, since delete[] only takes one expression.

    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  8. #8
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    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
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  2. private overloading of new & delete
    By Aidman in forum C++ Programming
    Replies: 12
    Last Post: 04-28-2005, 01:11 PM
  3. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  4. Replies: 3
    Last Post: 12-06-2002, 09:02 AM
  5. int vs BIG INT
    By rumi_red in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 03:15 AM

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