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
how can i easily make a program?

A palindrome is a string that reads the same from the both the ends. Given a string S convert it to a palindrome by doing character replacement. Your takes is to convert S to palin

: Write a program that prompts the user to enter five digit positive numbers. The program then outputs the digits of the number one digit per line. Eg if the user enters 32456, th

Comments Comments are integral part of every program . Comments help in coding, debugging and maintaining a program . The compiler ignores them. They should be used liberally i

It''s that spooky time of year when the dead walk the Earth, things go bump in the night, and the Centers for Disease Control (CDC) recognizes the need for zombie preparedness (

#question.College life is tough. Eating pizza for every meal is hitting you hard. You are looking at working out to stay healthy. You found a web site that tells you how many ca

The STL details are described in many places online (see the CS377 webpage for some links), and there's a very quick introduction in Section A.14. Here are just a few additional no


C program to Print Fibonacci series upto n using recursion: int fibo(long int); void main() {                 long int a=0,n;                 printf ("how many term

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b