how to properly delete a pointer to array

后端 未结 6 2066
无人共我
无人共我 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:03

    It's:

    delete[] foo;
    

    Edit: Modern compilers really need you to use the correct delete operator, or they may leak memory or fail to run the proper destructors.

    (I earlier stated that it didn't make a difference in practice. About 10 years ago, I tried many different compilers and failed to find one that actually leaked memory if you mistakenly omitted the []. However the howls of the mob - see below - have forced me to re-try my experimentation, and it seems that modern compilers/libc++s really do care.)

提交回复
热议问题