Memory Exhaustion
Although modern machines tend to have huge memory capacity, it is always possible that the free store will be exhausted. If the program uses all of available memory, then it is possible for a new expression to fail. If the new expression cannot acquire the requested memory, it throws an exception named bad_alloc. We’ll look at how exceptions are thrown in Section 6.13 (p. 215).
Destroying Dynamically Allocated Objects
When our use of the object is complete, we must explicitly return the object’s memory to the free store. We do so by applying the delete expression to a pointer that addresses the object we want to release.
delete pi;
frees the memory associated with the int object addressed by pi.
It is illegal to apply delete to a pointer that addresses memory that was not allocated by new.
注意:delete 的实参必须是new 返回的指针。
The effect of deleting a pointer that addresses memory that was not allocated by new is undefined. The following are examples of safe and unsafe delete expressions:
- int i;
- int *pi = &i;
string str = "dwarves"; double *pd = new double(33); delete str; delete pi; delete pd;
It is worth noting that the compiler might refuse to compile the delete of str. The compiler knows that str is not a pointer and so can detect this error at compiletime. The second error is more insidious: In general, compilers cannot tell what kind of object a pointer addresses. Most compilers will accept this code, even though it is in error.
delete of a Zero-Valued Pointer
It is legal to delete a pointer whose value is zero; doing so has no effect:
- int *ip = 0;
- delete ip;
The language guarantees that deleting a pointer that is equal to zero is safe.
Resetting the Value of a Pointer after a delete
When we write
- delete p;
p becomes undefined. Although p is undefined, on many machines, p still contains the address of the object to which it pointed. However, the memory to which p points was freed, so p is no longer valid.
After deleting a pointer, the pointer becomes what is referred to as a dangling pointer. A dangling pointer is one that refers to memory that once held an object but does so no longer. A dangling pointer can be the source of programerrors that
are difficult to detect.
Setting the pointer to 0 after the object it refers to has been deleted makes it clear that the pointer points to no object.
这其实用处不大,因为指针是可以拷贝的,delete p;之后再令p = NULL ;并不能保证没有其他指针指向这块已经被释放的内存。
Dynamic Allocation and Deallocation of const Objects
It is legal to dynamically create const objects:
-
- const int *pci = new const int(1024);
Like any const, a dynamically created const must be initialized when it is created and once initialized cannot be changed. The value returned from this new expression is a pointer to const int. Like the address of any other const object, the return from a new that allocates a const object may only be assigned to a pointer to const.
A const dynamic object of a class type that defines a default constructor may be initialized implicitly:
-
- const string *pcs = new const string;
This new expression does not explicitly initialize the object pointed to by pcs. Instead, the object to which pcs points is implicitly initialized to the empty string. Objects of built-in type or of a class type that does not provide a default constructor must be explicitly initialized.