how to properly delete a pointer to array

后端 未结 6 2111
无人共我
无人共我 2020-12-03 15:29

I\'m new to C++, and I\'m confused about arrays and pointers. Could someone tell me how I can properly delete a pointer. Like for example,

int *foo;
foo = ne         


        
6条回答
  •  没有蜡笔的小新
    2020-12-03 15:51

    You walked in the front door and left it open. Then you walked through to the back door and tried to close it but you smashed it because it was already closed.

    foo = new int [10];  // so when you open the front door
    delete [] foo;       // please remember to close the front door.
    
    foo = new int;       // and when you open the back door
    delete foo;          // please remember to close the back door.
    

    Don't mix them up!

提交回复
热议问题