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
In this problem you are given a board in which some of the elements are placed as shown in diagram below. Each element represents a color. Fill the other elements in the board, suc

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

I am seeking an expert to create a 360 degree virtual tour in KRPano for use on PCs, tablets and smartphones. You will be given with panoramas in order to create an advanced KRPano

Incomplete types refer to pointers in which there is no availability of the execution of the referenced location or it points to some location whose value is not available for modi

what is an array?

example of program to ad two numbers

What is constructor or ctor? need help

Explain the Bitwise-Inclusive-OR Operator: |? The bitwise-inclusive-OR operator (|) contrast each bit of its first operand to the corresponding bit of its second operand and if

After p = new Fred[n], how does the compiler know about n objects to be destructed throughout delete[] p? A:  The run-time system hold the number of objects, n, somewhere where

Write a program called plus that will display asterisks in the shape of a plus sign which has the height and width specified by the user. You must only allow entry of an odd numb