In this post, we will see Pointer and Reference Types | PPL | Sebesta | Data Types in Programming Language | data types in ppl, data types ppl, elementary data types in programming language, ppl, sebesta, pointer and reference types
9. Pointer and reference
Type
Definition:
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.
Dangling Pointer
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.
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.
Watch following video:
Watch on YouTube: https://www.youtube.com/watch?v=taPeDr8GNA0
No comments:
Post a Comment