The document discusses pointer and reference types in programming, highlighting the characteristics and operations of pointers, including assignment, dereferencing, and issues like dangling pointers and lost heap-dynamic variables. It contrasts pointers, which refer to memory addresses, with reference types that refer directly to objects or values in memory. Key design issues and problems associated with pointers are also outlined, emphasizing the importance of managing dynamic memory effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views17 pages
Problems Associated With Pointers
The document discusses pointer and reference types in programming, highlighting the characteristics and operations of pointers, including assignment, dereferencing, and issues like dangling pointers and lost heap-dynamic variables. It contrasts pointers, which refer to memory addresses, with reference types that refer directly to objects or values in memory. Key design issues and problems associated with pointers are also outlined, emphasizing the importance of managing dynamic memory effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17
Pointer and reference Type
• A pointer type is one in which the variables
have a range of values that consists of memory addresses and a special value, nil. • A pointer can be used to access a location in an area where storage is dynamically allocated called a heap. • Variables that are dynamically allocated from the heap are called heap-dynamic variables. Design Issues
• What are the scope and lifetime of a pointer
variable? • What is the lifetime of a heap-dynamic variable (the value a pointer references)? • Are pointers restricted as to the type of value to which they can point? • Are pointers used for dynamic storage management, indirect addressing, or both? • Should the language support pointer types, reference types, or both? Pointer Operations
two fundamental pointer operations:
• assignment and dereferencing • The first operation sets a pointer variable’s value to some useful address. • An occurrence of a pointer variable in an expression can be interpreted in two distinct ways. 1. it could be interpreted as a reference to the contents of the memory cell to which it is bound, which in the case of a pointer is an address. 2. the pointer could also be interpreted as a reference to the value in the memory cell pointed to by the memory cell to which the pointer variable is bound • In this case, the pointer is interpreted as an indirect reference. • This is known as dereferencing the pointer. • Dereferencing of pointers can be either explicit or implicit. • In Fortran 95+ it is implicit • In C++, it is explicitly specified with the asterisk (*) as a prefix unary operator. • Consider the following example of dereferencing: If ptr is a pointer variable with the value 7080 and the cell whose address is 7080 has the value 206, then the assignment j = *ptr sets j to 206. problems associated with pointers • Dangling Pointers • Lost Heap-Dynamic Variables Dangling Pointers
• A dangling pointer, or dangling reference, is a
pointer that contains the address of a heap- dynamic variable that has been deallocated • Example from C++ Language: • int * ptr1; • int * ptr2 = new int[100]; • ptr1 = ptr2; • delete [] ptr2; • Here, ptr1 and ptr2, both will be dangling pointers. • Java class instances are implicitly deallocated (there is no explicit deallocation operator), there cannot be dangling references in Java. The following sequence of operations creates a dangling pointer in many languages: 1. A new heap-dynamic variable is created and pointer p1 is set to point at it. 2. Pointer p2 is assigned p1’s value. 3. The heap-dynamic variable pointed to by p1 is explicitly deallocated (possibly setting p1 to nil), but p2 is not changed by the operation. p2 is now a dangling pointer. If the deallocation operation did not change p1, both p1 and p2 would be dangling. (Of course, this is a problem of aliasing—p1 and p2 are aliases.) Lost Heap-Dynamic Variables
• A lost heap-dynamic variable is an allocated
heap-dynamic variable that is no longer accessible to the user program. Such variables are often called garbage, because they are not useful for their original purpose, and they also cannot be reallocated for some new use in the program. Lost heap-dynamic variables are most often created by the following sequence of operations: 1. Pointer p1 is set to point to a newly created heap-dynamic variable. 2. p1 is later set to point to another newly created heap-dynamic variable. The first heap-dynamic variable is now inaccessible, or lost. This is sometimes called memory leakage. • Reference Type: • A reference type variable is similar to a pointer, with one important and fundamental difference: A pointer refers to an address in memory, while a reference refers to an object or a value in memory. • Example: • int a = 0; • int &b = a; • b = 100; • In this code segment, variables a and b are aliases. b is reference to a.