Can i drop the [] while deleteing array of some built-in , C/C++ Programming

Can I drop the [] while deleteing array of some built-in type (char, int, etc)?

A: No. you can't

Sometimes programmers think that the [] in the delete[] p only present so the compiler will call the suitable destructors for all elements in the array. Due to this reasoning, they suppose that an array of some built-in type such as char or int can be deleted without the []. For example they suppose the following is valid code:

void userCode(int n)

{

char* p = new char[n];

...

delete p; //  ERROR! Should be delete[] p !

}

However the above code is wrong and it can cause a blow at runtime. Particularly, the code that's called for delete p is operator delete (void*), however the code that's called for delete[] p is operator delete[](void*). For the latter the default behavior is to call the former, however users are allowed to replace the latter along with a different behavior (in which case normally they would also replace the corresponding new code in operator new[](size_t)). If they replaced the delete[] code thus it wasn't compatible  along with the delete code, and you called the wrong one (that means  if you said delete p instead of delete[] p), you could end up with a disaster at runtime.

Posted Date: 3/19/2013 8:44:57 AM | Location : United States







Related Discussions:- Can i drop the [] while deleteing array of some built-in , Assignment Help, Ask Question on Can i drop the [] while deleteing array of some built-in , Get Answer, Expert's Help, Can i drop the [] while deleteing array of some built-in Discussions

Write discussion on Can i drop the [] while deleteing array of some built-in
Your posts are moderated
Related Questions
Implementation of the Dictionary class: int Dictionary::find_word(char *s) {    char word[81];    for (int i = 0; i       if (stricmp(words[i].get_word(word),s) =


Develop libcurl XML project I have a Project using XML with C++. You will use things like LIbcurl etc....and a XML parser to finish the project. Developer must know how to use X

THIS PROGRAM IS TO ADD TWO DISTANCES & SHOW THEIR SUM AS NEW DISTANCE # include # include typedef int dist; struct distance   {   dist inch;   dist feet;   }; distance add

THIS PROGRAM IS TO DISPLAY A STOCK CONTAINING ITEM CODE,ITEM NAME,PRICE AND ANOTHER STOCK WITH CODE & QUANTITY AND DISPLAY COMPLETE INFORMATION #include #include #include

Program to find a Greatest String: C - Program: Write a program find largest string by c program. int main( int argc, char *argv[] ) {     if( argc         {

Write a function that calculates the number of elapsed days between two dates. For example the days between Feb 3, 1970 and June 21, 1980? Becareful for the Leap year

Dereferencing the Pointer this Sometimes a member function requires to make a copy of the invoking instance so that it can change the copy without affecting the original instan

Project Description: We are seeking online invoice OCR solution. That will help us to automate accounts payable function. User should be able to use the offered pdf file or s

write a program to find the area under curve y=f(x) between x=a and x=b,integrate y=f(x) between the limits a and b