Pointers and Reference Types

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

*unsafe*

(on practice final) C# includes both the references of Java and the pointers of C++. However, any subprogram that uses pointers must include the ________ modifier.

*lost heap-dynamic variable, garbage*

A ________ ________ ________ is a heap-dynamic variable that is no longer accessible to a program. Such variables are often called ________ because they are no longer useful, yet the space they occupy cannot be reallocated.

*dangling pointer, dangling reference* • Reasons why dangling pointers are dangerous: 1. The location being pointed to may have been reallocated to some new heap- dynamic variable. Changing the value stored at the location will destroy the value of the new variable. 2. The location may now be temporarily used by the storage management system, possibly as a pointer in a chain of available blocks of storage. Changing the value stored at the location may cause the storage manager to fail. • C++ example: int *arrayPtr1; int *arrayPtr2 = new int[100]; arrayPtr1 = arrayPtr2; delete [] arrayPtr2; // arrayPtr1 and arrayPtr2 are dangling

A ________ ________, or ________ ________, is a pointer that contains the address of a heap-dynamic variable that has been deallocated.

*reference type* • A C++ reference variable is essentially an alias for another variable. It must be initialized at the time it is bound to storage, and it cannot refer to any other variable during its lifetime. • The declaration of a reference variable contains an ampersand (&): • example: int result = 0; int& ref_result = result; ... ref_result = 100; • ref_result is an alias for result. • In C++, reference types are used primarily for declaring parameters. • In Java, reference variables behave more like pointer variables that are implicitly dereferenced. • Properties of Java references: Must refer to objects. Can be modified to refer to different objects at different times. • Because Java objects are implicitly deallocated, dangling references are impossible. • All variables in Smalltalk, Python, Ruby, and Lua store references and are implicitly dereferenced.

A value of a/an ________ ________ is similar to a pointer, except that it must refer to an object or other value in memory. Arithmetic cannot be performed on <answer1>.

*assignment, dereferencing* • Dereferencing can be either explicit or implicit. In many contemporary lan- guages it is explicit.

Languages that provide a pointer type usually include two fundamental pointer operations: ________ and _________. <answer1> acts as a reference to the variable itself. <answer2> acts as an indirect reference to the memory cell that the variable points to. (Accessing this cell is called <answer2> the pointer.)

*malloc, new* • Some languages also provide an explicit deallocation operation, such as "delete" in C++.

Languages that provide pointers for heap allocation must include an explicit allocation operation, which may be a subprogram (________ in C) or an operator (often called ________).

*Tombstone, Lock-and-Key* TOMBSTONE: • All heap-dynamic variables are accompanied by a special cell, called a "tombstone", that points to the heap-dynamic variable. Pointer variables point only at tombstones and never to heap-dynamic variables. When a heap-dynamic variable is deallocated, the tombstone remains but is set to nil, indicating that the heap-dynamic variable no longer exists. • The tombstone technique prevents a pointer from ever pointing to a deallocated variable. A reference to a pointer that points to a nil tombstone can be detected as an error. • Disadvantages of tombstones: Tombstones require extra space that can never reclaimed. Accessing a heap-dynamic variable requires an extra level of indirection. As a result, no widely used language uses tombstones. LOCK-AND-KEY: • used in the implementation of UW-Pascal. • A pointer is stored as a (key, address) pair, where the key is an integer value. Each heap-dynamic variable has a header cell that stores an integer lock value. • When a heap-dynamic variable is allocated, a lock value is created and placed both in the lock cell of the heap-dynamic variable and in the key cell of the pointer that is specified in the call to new. • Every access to a dereferenced pointer compares the key value of the pointer to the lock value in the heap-dynamic variable. If they match, the access is legal; otherwise, the access is treated as a run-time error. • When a heap-dynamic variable is deallocated with "dispose" function, its lock value is set to an illegal lock value.

List solutions to Dangling Pointer Problem. ________ ________.

*memory leakage*

The problem of lost heap-dynamic variables (garbage) is sometimes called ________ ________.

*pointer* • Uses of pointers include: Indirect addressing Accessing anonymous variables in the heap • Having pointers in a language improves its writability. Implementing a dynamic structure (a binary tree, for example) is difficult in a language that does not have pointers or dynamic storage. • C++ Examples: int list[10]; int *ptr; ... ptr = list; • ptr is assigned the address of list[0]. After this assignment, the following are equivalent: *(ptr + 1) and list[1] *(ptr + index) and list[index] ptr[index] and list[index] • Pointers have two additional uses in C and C++: Pointers can point to functions. Function parameters are often pointers, allowing functions to modify arguments passed to them. • C and C++ allow pointers of type void *, which can point at values of any type. Functions that deal with arbitrary memory addresses often have void \* parameters. • In C++, dereferencing is explicitly specified by using the asterisk (*) as a prefix unary operation. Example: j = *ptr; • If ptr is a pointer variable with the value 7080, and the cell whose address is 7080 has the value 206, then the assignment sets j to 206 • When pointers point to records, the syntax of references to fields of these records varies. In C and C++, if p points to a structure with a field named age, then (*p).age can be used to refer to that field. The expression p->age can be used as an alternative. The -> operator combines dereferencing and field reference. • Some recent languages, such as Java, have replaced pointers completely with reference types, which, along with implicit deallocation, minimize the primary problems with pointers.

The values of a ________ type consist of memory addresses and a special value, nil. nil is not a valid address; it indicates that a <answer> cannot currently be used to reference any memory cell.


संबंधित स्टडी सेट्स

Module 4: Market Failures: Public Goods and Externalities

View Set

Chapter 6 Communication in the Life Cycle of Relationships

View Set

253 - Neuro Questions (51-100 with rationales and strategies)

View Set

Central Nervous System Test Review Exam 2

View Set

Test - 3,11,12 American Government :)

View Set