TAILIEUCHUNG - Absolute C++ (4th Edition) part 42

Absolute C++ (4th Edition) part 42. KEY BENEFIT: C++ programming concepts and techniques are presented in a straightforward style using understandable language and code. KEY TOPICS: C++ Basics; Flow of Control; Function Basics; Parameters and Overloading; Arrays; Structures and Classes; Constructors; Operator Overloading, Friends, and References; Strings; Pointers and Dynamic Arrays; Separate Compilation and Namespaces; Streams and File I/O; Recursion; Inheritance; Polymorphism and Virtual Functions; Templates; Linked Data Structures; Exception Handling; Standard Template Library; Patterns and UML. MARKET: Useful for both beginning and intermediate C++ programmers. . | 416 Pointers and Dynamic Arrays The delete Operator The delete operator eliminates a dynamic variable and returns the memory that the dynamic variable occupied to the freestore. The memory can then be reused to create new dynamic variables. For example the following eliminates the dynamic variable pointed to by the pointer variable p delete p After a call to delete the value of the pointer variable like p above is undefined. A slightly different version of delete discussed later in this chapter is used when the dynamically allocated variable is an array. Pitfall Dangling Pointers dangling pointer When you apply delete to a pointer variable the dynamic variable to which it is pointing is destroyed. At that point the value of the pointer variable is undefined which means that you do not know where it is pointing. Moreover if some other pointer variable was pointing to the dynamic variable that was destroyed then this other pointer variable is also undefined. These undefined pointer variables are called dangling pointers. If p is a dangling pointer and your program applies the dereferencing operator to p to produce the expression p the result is unpredictable and usually disastrous. Before you apply the dereferencing operator to a pointer variable you should be certain that the pointer variable points to some variable. C has no built-in test to check whether a pointer variable is a dangling pointer. One way to avoid dangling pointers is to set any dangling pointer variable equal to NULL. Then your program can test the pointer variable to see if it is equal to NULL before applying the dereferencing operator to the pointer variable. When you use this technique you follow a call to delete by code that sets all dangling pointers equal to NULL. Remember other pointer variables may become dangling pointers besides the one pointer variable used in the call to delete so be sure to set all dangling pointers to NULL. It is up to the programmer to keep track of dangling pointers

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.