how to properly delete a pointer to array

后端 未结 6 2107
无人共我
无人共我 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 16:01

    If you allocate an array of objects using the new [] operator then you must use the delete [] operator and therefore the non-array new can only be used with the non-array delete.

    int *a = new int[10];
    int *b = new int;
    
    delete [] a;
    delete b;
    

提交回复
热议问题