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
write an algorithm for multiplication of two sparse matrices using linked lists

Academic expert for Genetic Algorithm Project Description: Seeking academic expert for Genetic Algorithm. Skills required are Academic Writing, Algorithm, C++ Programming,

Define the Data Type Qualifiers in c Language? 1. const 2. volatile const: The const qualifier is used to tell C that the variable value can't change after initialization.

Op e r a t o r O v e r l o a d i n g : W h e n   a n   o p e r a t o r   i s   u s e d   f o r   d i ff e r e n t   op e r a

Access privileges 1. If the designer of the base class needs no one, not even a derived class to access a member, then that member should be made private. 2. If the designer

program for decode the encoded numbering format into message

Write algorithm and program for the conversion of a Tree to a Binary Tree

when you allocate memory with new[], you ought to free the memory via delete[]. While you allocate memory along 'new', then use 'delete' with no the brackets. You employ new[] to a

A: A dangling pointer arises while you use the address of an object after its lifetime is end. It may occur in situations such as returning addresses of automatic variables from a

A:BinaryHeap.h ------------ #ifndef BINARY_HEAP_H_ #define BINARY_HEAP_H_ #include "dsexceptions.h" #include "vector.h" // BinaryHeap class // CONSTRUCTION: wi