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
Generate a class node that contains an integer id, a position (x, y)  and a vector of 0 5,  generate a set of  x nodes each with random connectivity n.    Implement an algorith

i have a project in BDD(Binary Decision Diagram).. where i have to use BDD library...i m using cudd package which uses BDD technique...i download cudd package..try to run in vc++ 6

Selection sort - C program: Write a program to define a selection sort. void main()  {   clrscr();   int a[100],ch,n;   cout   cin>>n;   for (int i=0;i

For this program you will add and test 2 new member functions to the IntSLList class posted on the website. The two member functions are: insertByPosn(int el, int pos) Assuming t

A: In C++ the access privileges are public, private and protected. The default access level assigned to members of a class is private. Private members of any class are accessible o

construct a console programme for a bank ATM machine.

within 2 mins give me answer pllzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

You are required to write a stand-alone DSP application in the C programming language. The overall objective of this application is to convert a signal from the time domain to t


c++ coding decode the letters from numbers