tailieunhanh - Lecture note Data visualization - Chapter 10

In this chapter, the following content will be discussed: Pointer syntax in C++, null pointers, dynamic memory allocation, the new operator, garbage collection and delete operator. | Lecture 10 Recap Pointer Syntax in C++ Null Pointers Dynamic Memory Allocation The new Operator Garbage Collection and delete Operator Stale Pointers & Double Delete A problem may arise while using pointers that is an object may have several pointers pointing to it string *s = new string( "hello" ) ; / / s points at new string string *t = s; / / t points there, too delete t; / / The object is gone Assume these lines are scattered in a program as nobody write these lines of codes next to each other Prior to the call to delete, we have one dynamically allocated object that has two pointers pointing to it Continued . After the call to delete , values of s and t are unchanged They now are stale A stale pointer is a pointer whose value no longer refers to a valid object Dereferencing s and t can lead to unpredictable results Difficulty is that although t is obviously stale, the fact that s is stale is much less obvious, according to assumed situation Furthermore, in some situations, the . | Lecture 10 Recap Pointer Syntax in C++ Null Pointers Dynamic Memory Allocation The new Operator Garbage Collection and delete Operator Stale Pointers & Double Delete A problem may arise while using pointers that is an object may have several pointers pointing to it string *s = new string( "hello" ) ; / / s points at new string string *t = s; / / t points there, too delete t; / / The object is gone Assume these lines are scattered in a program as nobody write these lines of codes next to each other Prior to the call to delete, we have one dynamically allocated object that has two pointers pointing to it Continued . After the call to delete , values of s and t are unchanged They now are stale A stale pointer is a pointer whose value no longer refers to a valid object Dereferencing s and t can lead to unpredictable results Difficulty is that although t is obviously stale, the fact that s is stale is much less obvious, according to assumed situation Furthermore, in some situations, the memory that was occupied by the object is unchanged until a later call to new claims the memory, which can give the illusion that there is no problem Continued . A double-delete occurs when we attempt to call delete on the same object more than once It would occur if we now made the call delete s; // Oops -- double delete The above statement may be invalid because s is stale and the object that it points to is no longer valid Trouble in the form of a run-time error is likely to result Continued . We must be certain never to call delete more than once on an object-and then only after we no longer need it If we don't call delete at all, we get a memory leak And if we have a pointer variable and intend to call delete, we must be certain that the object being pointed at was created by a call to new When we have functions calling functions calling other functions, keeping track of everything is not so easy Reference Variables A reference type is an alias and may be viewed as a pointer .

crossorigin="anonymous">
Đã 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.