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 = new int[10];
delete foo;
or
delete [] foo;
Thanks.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With