delete vs. delete []
-
Hi! I have a question concerning the use of delete keyword in C++ programs. First, I do something like this:
int *pInt = new int [16]; delete pInt;
All the documentation I have read about delete sais that you must use delete [] when the allocated memory is an array. But if I run the small program containing these two lines of code in Debug mode, the _CrtDumpMemoryLeaks function tells me that the is no memory leak. In conclusion, why is there any need to deallocate the memory pointed by pInt using delete [] pInt; if it works using only delete pInt? If anyone can answer my question, I would greatly appreciate it! Thanks in advance! -
Hi! I have a question concerning the use of delete keyword in C++ programs. First, I do something like this:
int *pInt = new int [16]; delete pInt;
All the documentation I have read about delete sais that you must use delete [] when the allocated memory is an array. But if I run the small program containing these two lines of code in Debug mode, the _CrtDumpMemoryLeaks function tells me that the is no memory leak. In conclusion, why is there any need to deallocate the memory pointed by pInt using delete [] pInt; if it works using only delete pInt? If anyone can answer my question, I would greatly appreciate it! Thanks in advance! -
Thank you! It helps a lot!
-
Hi! I have a question concerning the use of delete keyword in C++ programs. First, I do something like this:
int *pInt = new int [16]; delete pInt;
All the documentation I have read about delete sais that you must use delete [] when the allocated memory is an array. But if I run the small program containing these two lines of code in Debug mode, the _CrtDumpMemoryLeaks function tells me that the is no memory leak. In conclusion, why is there any need to deallocate the memory pointed by pInt using delete [] pInt; if it works using only delete pInt? If anyone can answer my question, I would greatly appreciate it! Thanks in advance!Eikthrynir wrote:
lete [] pInt
actually [] instruct compiler to delete the array of Memory block here.. if you use simple delete then only one memory block will be freed
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and you