|
If p is an array that points to a chunk that is in the heap, then statement
delete [ ] p;deletes the chunk that p points to. You are required to use an empty pair of brackets after delete if you allocated p's chunk using new T[size ] for some type T and expression size. If p points to a variable that was allocated using new T, then you are required not to write an empty pair of brackets after delete. It is up to you do know which to use.
You cannot delete part of a chunk. Only use delete p or delete [] p if p is a pointer that was given to you using new. Deleting a pointer that was not given to you by new will corrupt the heap manager.
At this point, it should go without saying that deleting an array A that is allocated as
T A[size];will corrupt the heap manager. That is never good.
Suppose array A has been created by
double* A = new double[m];If you are done with array A and want to return its chunk to the heap manager, what statement should you use? Answer
Suppose array A has been created by
double A[m];
When the program is done with array A,
what should the program do?
Answer
There is something wrong with the following function. Explain what is wrong.
// makeZeroedArray(n) returns an array with // n items, all set to 0. int* makeZeroedArray(int n) { int* p = new int[n]; for(int i = 0; i < n; i++) { p[i] = 0; } delete [] p; return p; }Answer
|