Why do I need to delete[]?

前端 未结 15 635
猫巷女王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: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.

提交回复
热议问题