Why do I need to delete[]?

2019-01-21 18:26发布

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
    }

    delete[] str;
    return 0;
}
  1. Why would I need to delete str if I am going to end the program anyways? I wouldn't care if that memory goes to a land full of unicorns if I am just going to exit, right?

  2. Is it just good practice?

  3. Does it have deeper consequences?

15条回答
我命由我不由天
2楼-- · 2019-01-21 19:20

Most operating systems will reclaim memory upon process exit. Exceptions may include certain RTOS's, old mobile devices etc.

In an absolute sense your app won't leak memory; however it's good practice to clean up memory you allocate even if you know it won't cause a real leak. This issue is leaks are much, much harder to fix than not having them to begin with. Let's say you decide that you want to move the functionality in your main() to another function. You'll may end up with a real leak.

It's also bad aesthetics, many developers will look at the unfreed 'str' and feel slight nausea :(

查看更多
Juvenile、少年°
3楼-- · 2019-01-21 19:21

Why would I need to delete str if I am going to end the program anyways?

Because you don't want to be lazy ...

I wouldn't care if that memory goes to a land full of unicorns if I am just going to exit, right?

Nope, I don't care about the land of unicorns either. The Land of Arwen is a different matter, Then we could cut their horns off and put them to good use(I've heard its a good aphrodisiac).

Is it just good practice?

It is justly a good practice.

Does it have deeper consequences?

Someone else has to clean up after you. Maybe you like that, I moved out from under my parents' roof many years ago.

Place a while(1) loop construct around your code without delete. The code-complexity does not matter. Memory leaks are related to process time.

From the perspective of debug, not releasing system resources(file handles, etc) can cause more significant and hard to find bugs. Memory-leaks while important are typically much easier to diagnose(why can't I write to this file?). Bad style will become more of a problem if you start working with threads.

int main()
{

    while(1)
    { 
        char* str = new char[10];

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

    delete[] str;
    return 0;
}
查看更多
我只想做你的唯一
4楼-- · 2019-01-21 19:30

Not to mention that if you are going to apply for a job as a C++ programmer there is a very good chance that you won't get past the interview because of the missing delete. First - programmers don't like any leaks usually (and the guy at the interview will be surely one of them) and second - most companies (all I worked in at least) have the "no-leak" policy. Generally, the software you write is supposed to run for quite a while, creating and destroying objects on the go. In such an environment leaks can lead to disasters...

查看更多
登录 后发表回答