Thread: Question about delete function

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    delete[] frees whatever memory was allocated with new[], whatever was allocated.

    If you used new to allocate, use delete to free.
    If you used new[] to allocate, use delete[] to free.
    Even better, don't use new/delete at all. Use containers and make_unique/make_shared.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    freddie, any exception can be replaced with a bool wasThereAnError member of a class and then checked later. I've done this refactoring myself in some cases when C++ exceptions are not supported by the runtime environment. However, keep in mind that it adds an extra field to every single String object in your program. This additional space can be prohibitive (or at least not well utilized) especially when you have lots of small strings... and we know that runtimes already go to a lot of effort to optimize for short strings c++ - What are the mechanics of short string optimization in libc++? - Stack Overflow.

    It's true that sometimes C++ exceptions go against the coding style of a codebase or may require additional runtime/buildtime cost. For instance, when compiling shared libraries that may want to throw exceptions, g++ may need the flag -shared-libgcc because the static libgcc.a doesn't support this. But even when you forget to link with this flag, or you're messing with the linker and you forget to have a .eh_frame exception header section in your executable (I may have done this recently...), an exception will simply cause a SIGABRT. Which will kill your program, but you will see a useful stack trace in a debugger. I think that's preferable to wasting space in every String object, getting NULL pointers, and potentially only getting a crash later when you try to use a large String object -- assuming you don't expect to commonly check for string allocation errors. Keep in mind also that if you configure new to throw exceptions you'll get the same behaviour as well.

    Well actually, I think it's best to stick with the standard std::string unless you really have a need to do otherwise; clearly you like to use Windows allocation functions and so our needs are different. Just some food for thought.
    Last edited by dwks; 08-10-2016 at 08:55 AM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  2. List - Why my delete function doesn't delete?
    By juanjuanjuan in forum C Programming
    Replies: 7
    Last Post: 12-09-2014, 10:10 PM
  3. BST delete function
    By spikestar in forum C++ Programming
    Replies: 0
    Last Post: 08-31-2010, 08:55 PM
  4. Delete Function for AVL Trees
    By Lost in forum C Programming
    Replies: 5
    Last Post: 08-24-2009, 10:34 AM
  5. does C have a delete function??
    By aspand in forum C Programming
    Replies: 12
    Last Post: 05-17-2002, 03:14 PM

Tags for this Thread