delete[]

This is a discussion on delete[] within the C++ Programming forums, part of the General Programming Boards category; Is it OK to delete a list of objects or arrays in a single command, i.e. Code: delete[] arr1, arr2, ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    595

    delete[]

    Is it OK to delete a list of objects or arrays in a single command, i.e.
    Code:
    delete[] arr1, arr2, arr3;
    It compiles, but I can't find this form in any references and I'm not convinced that it's actually freeing the memory.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Have you turned warnings on. With MingW I get "right-hand operand of comma has no effect". So it compiles because it's the comma operator you are using, except it doesn't do anything for arr2 and arr3.

    Comma operator means: evaluate all operands from left to right, discard all results except right-most and return it.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect that will delete arr3, and do nothing useful with arr2 and arr1. I may have it the other way around, and it deletes arr1 then does nothing useful with arr2 and arr3 - but it certainly doesn't delete all three. You need a line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Of course, there's very little reason to ever use delete[] in a C++ program at all. Why aren't you using vector?

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Thanks. I see that if I compile using the -Wall option, gcc gives me
    "warning: right-hand operand of comma has no effect"
    for each operand after the first, so clearly a separate line is needed for each.

    Quote Originally Posted by Daved View Post
    Of course, there's very little reason to ever use delete[] in a C++ program at all. Why aren't you using vector?
    I'm writing some robot-vision image processing code. I'm often addressing pixels at specific computed locations on the image rather than iterating through in raster order, so iterators didn't seem useful. Pointer arithmetic on an ordinary array feels like a more natural way to move around the image. I realize that vector.at() allows addressing specific locations, but wouldn't vector just be adding extra overhead for no particular reason? I'm trying to keep this dog working in real-time, as close to 30 frames per second as possible, so I'd like to avoid any unnecessary burden. Am I wrong?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    On most implementations vector's operator[] behaves the same as operator[] with dynamic arrays. (One exception is recent versions of Visual C++ but you can turn off it's extra range checking.)

    vector adds very little overhead, but there is a particular reason to use it. You don't have to worry about delete []. You don't have to worry about making your classes copy-safe and you don't have to worry about exceptions (if you use them) causing memory leaks. You probably have most of these things under control, but in most cases (all that I've seen) there is no negative to using the vector versus a dynamic array with new[] and delete[].

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Thanks for your input. I'll try to keep that in mind.

    Not that I have any reason to mistrust you, but this is supposed to be research, so I should test something both ways to see how much, if anything, vector adds to the running time. When I get around to doing that I'll post a follow-up.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by R.Stiltskin
    I'm often addressing pixels at specific computed locations on the image rather than iterating through in raster order, so iterators didn't seem useful. Pointer arithmetic on an ordinary array feels like a more natural way to move around the image.
    The iterators of a std::vector are random access iterators, just like pointers, so there would be minimal difference. If you want to use raw pointers, &vec[0] for a std::vector named vec will give you a raw pointer to the first element of the std::vector.
    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

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Of course, I wouldn't expect you to just take my word for it.

    A common rule in programming is not to optimize prematurely. Because vector is standard and is safer and more robust than a plain dynamic array, I would generally suggest using it first and then find out if it is causing you any slowdowns (I'd be surprised if it did compared to a plain dynamic array). If you've already started coding your project and you're not familiar with vector, then going the other direction is perfectly fine.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by R.Stiltskin View Post
    Thanks for your input. I'll try to keep that in mind.

    Not that I have any reason to mistrust you, but this is supposed to be research, so I should test something both ways to see how much, if anything, vector adds to the running time. When I get around to doing that I'll post a follow-up.
    Chapter 76. Use vector by default. Otherwise, choose an appropriate container
    Quote Originally Posted by C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
    If you doubt this advice, ask yourself if you really have a compelling reason not to use the only standard container that guarantees all of the following properties. vector alone is:

    Guaranteed to have the lowest space overhead of any container (zero bytes per object).

    Guaranteed to have the fastest access speed to contained elements of any container.

    Guaranteed to have inherent locality of reference, meaning that objects near each other in the container are guaranteed to be near each other in memory, which is not guaranteed by any other standard container.

    Guaranteed to be layout-compatible with C, unlike any other standard container. (See Items 77 and Chapter 78)

    Guaranteed to have the most flexible iterators (random access iterators) of any container.

    Almost certain to have the fastest iterators (pointers, or classes with comparable performance that often compile away to the same speed as pointers when not in debug mode), faster than those of all other containers.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    cpjust, don't all of those properties relate to vector as compared to other standard containers, as opposed to raw arrays?

    I don't doubt that vector provides some very useful features. My question is what do they cost? It's hard to believe that all those benefits come for free. The cost may actually be "trivial", but what's trivial on a high-end workstation may not be trivial on a small robot with a MIPS R4000 processor and only 64MB of ram that has to handle camera and multiple sensor inputs, motor outputs, and multitask among vision, motion, localization, and planning modules.

    We've already had the thing choking on code that simply demanded too much, so while Daved's advice about not optimizing prematurely clearly has merit, I'm still jealous about these limited resources and want to convince myself that any added features pay for themselves.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Quote Originally Posted by R.Stiltskin
    I don't doubt that vector provides some very useful features. My question is what do they cost? It's hard to believe that all those benefits come for free. The cost may actually be "trivial", but what's trivial on a high-end workstation may not be trivial on a small robot with a MIPS R4000 processor and only 64MB of ram that has to handle camera and multiple sensor inputs, motor outputs, and multitask among vision, motion, localization, and planning modules.
    With 64 MB of RAM, you probably cannot afford memory leaks, and std::vector provides automatic, deterministic, memory management. Yes, there are book keeping costs (the container must keep track of its own size and capacity), but those are pretty much the same as you would have if you implemented a dynamic array container by hand (and possibly more efficient since you might say, default construct objects where std::vector delays construction until it is needed).

    Of course, if you need a fixed size array, then maybe you should go for std::tr1::array (or implement it yourself).
    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

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Write a test program that uses dynamically allocated arrays vs vectors and see what the speed difference is. I'm sure there would be an incredibly small difference, but I doubt it would be enough to cause a noticeable difference.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    vectors is like a container for a dynamic array with a lot of provided functions. Found in <vector>. So, if you don't mind having those provided useful functions stored in the RAM (which I believe are the size of kilobytes, so you wouldn't really mind) then you should use a vector. Just use the [] to iterate. Nothing more nothing less.

    Of course if you are allocating memory with a certain pattern then maybe an array might be better (like increase the capacity by 4 every time you insert a value). But that is probably unlikely on what you are doing. And you probably would use a container. And in the end you could just replace vector with myVector and be more flexible from the beginning.

    (I think there was a topic that I was "defending" new vs vector, or am I wrong?)

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Write a test program that uses dynamically allocated arrays vs vectors and see what the speed difference is. I'm sure there would be an incredibly small difference, but I doubt it would be enough to cause a noticeable difference.
    Or just take the results from my experiments here:
    Vector vs. array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete[] problem with release config...
    By mikahell in forum C++ Programming
    Replies: 8
    Last Post: 08-21-2006, 10:37 AM
  2. delete[] without new[]?
    By ChadJohnson in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2005, 11:39 AM
  3. delete[] or delete?
    By X PaYnE X in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2005, 02:16 PM
  4. Memory issue with new[] and delete[]
    By Zarkhalar in forum C++ Programming
    Replies: 24
    Last Post: 08-07-2004, 07:45 AM
  5. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM

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