Why do I need to delete[]?

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

    new and delete are reserved keyword brothers. They should cooperate with each other through a code block or through the parent object's lifecyle. Whenever the younger brother commits a fault (new), the older brother will want to to clean (delete) it up. Then the mother (your program) will be happy and proud of them.

提交回复
热议问题