Why do I need to delete[]?

前端 未结 15 637
猫巷女王i
猫巷女王i 2020-12-13 06:16

Lets say I have a function like this:

int main()
{
    char* str = new char[10];

    for(int i=0;i<5;i++)
    {
        //Do stuff with str
    }

    d         


        
相关标签:
15条回答
  • 2020-12-13 06:47

    I cannot agree more to Eric Lippert's excellent advice:

    So the answer to the question "should I free memory before my program exits?" is "it depends on what your program does".

    Other answers here have provided arguments for and against both, but the real crux of the matter is what your program does. Consider a more non-trivial example wherein the type instance being dynamically allocated is an custom class and the class destructor performs some actions which produces side effect. In such a situation the argument of memory leaks or not is trivial the more important problem is that failing to call delete on such a class instance will result in Undefined behavior.

    [basic.life] 3.8 Object lifetime
    Para 4:

    A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; however, if there is no explicit call to the destructor or if a delete-expression (5.3.5) is not used to release the storage, the destructor shall not be implicitly called and any program that depends on the side effects produced by the destructor has undefined behavior.

    So the answer to your question is as Eric says "depends on what your program does"

    0 讨论(0)
  • 2020-12-13 06:47

    It's a fair question, and there are a few things to consider when answering:

    • some objects have more complex destructors which don't just release memory when they're deleted. They may have other side effects, which you don't want to skip.
    • It is not guaranteed by the C++ standard that your memory will be released when the process terminates. (Of course on a modern OS it will be freed, but if you were on some weird OS which didn't do that, you'd have to free your memory properly
    • on the other hand, running destructors at program exit can actually take up quite a lot of time, and if all the do is release memory (which would be released anyway), then yes, it makes a lot of sense to just short-circuit that and exit immediately instead.
    0 讨论(0)
  • 2020-12-13 06:52

    Your Operating System should take care of the memory and clean it up when you exit your program, but it is in general good practice to free up any memory you have reserved. I think personally it is best to get into the correct mindset of doing so, as while you are doing simple programs, you are most likely doing so to learn.

    Either way, the only way to guaranteed that the memory is freed up is by doing so yourself.

    0 讨论(0)
提交回复
热议问题