3. OVERVIEW OF REFERENCES AND POINTERS
Often need to refer to another object
– Without making a copy of the object itself
– Also known as “aliasing”
Two ways to do this
– Directly, via a reference
o • Acts as an alias for the object
o • User interacts with reference as if it were the object itself
– Indirectly, via a pointer
o • Gives the address (in memory) of the object
o • Requires the user to do extra work: dereferencing
5. WHAT IS A REFERENCES
Reference is an alternative name for an existing variable (alias).
• A variable holding an address of what it “refers to” in memory
• But with a nicer interface
– An alias to the object
– Hides indirection from programmer
• Must be typed
– Checked by compiler
– Again can only refer to the type to which it can point
int &r = i; // can only refer to int
• Must always refer to something
– Must be initialized, cannot be changed
– More restricted than Java references
6. WHAT IS A POINTER
Pointers is simply a variable that maintain as a value the address of another location in
memory.
A variable holding an address of what it “points to” in memory
• Can be untyped
void * v; // can point to any type
• However, usually they’re typed
– Checked by compiler
– Can only be assigned addresses of variables of the type to which it can point
int * p; // can only point to int
• Addresses: garbage, something, nothing
– When created: int * p = i; vs. int * q;
q = 0; // now it points to nothing
p = NULL; // not portable, use nullptr instead
7. WHAT IS A POINTER
Pointers is simply a variable that maintain as a value the address of another location in
memory.
A variable holding an address of what it “points to” in memory
• Can be untyped
void * v; // can point to any type
• However, usually they’re typed
– Checked by compiler
– Can only be assigned addresses of variables of the type to which it can point
int * p; // can only point to int
• Addresses: garbage, something, nothing
– When created: int * p = i; vs. int * q;
q = 0; // now it points to nothing
p = NULL; // not portable, use nullptr instead
8. REFERENCE AND POINTER VARIABLES
Variable and object values are stored in particular locations in the computer’s memory.
Reference and pointer variables store the memory location of other variables.
Pointers are found in C. References are a C++ variation that makes pointers easier and safer to
use.
string hello = "Hello";
string *hello_ptr = &hello;
string &hello_ref = hello;
The object hello
occupies some
computer memory.
The asterisk indicates that hello_ptr is a
pointer to a string. hello_ptr variable is
assigned the memory address of object hello
which is accessed with the “&” syntax.
The & here indicates that hello_ref is a reference to a
string. The hello_ref variable is assigned the memory
address of object hello automatically.
REFERENCE AND POINTER VARIABLES
9. Difference between reference and pointers
A reference can never be null; it must always refer to a legitimate object.
Once established, a reference can never be changed to make it point to a different object.
A reference does not require any explicit mechanism to dereference the memory address and
access the actual data value.
REFERENCE AND POINTER VARIABLES
10. DYNAMIC MEMORY ALLOCATION
Dynamic memory allocation is the process of allocating and freeing memory during the execution
of a program.
It is useful when the memory needs of a program are not known in advance, or when they change
during the program’s lifetime.
It differs from static memory allocation, which is done at compile time and cannot be changed
afterwards.
11. DYNAMIC MEMORY ALLOCATION
Suppose we want to create an array of integers with a size that is determined by the user’s input.
We cannot use a regular array declaration, because the size must be a constant expression. Instead,
we can use dynamic memory allocation with pointers:
int *arr; // declare a pointer to int
int n; // declare an int variable to store the size
cout << "Enter the size of the array: ";
cin >> n; // read the size from the user
arr = new int[n]; // allocate memory for n elements
of type int and assign the address to arr
// now we can use arr as a regular array, for
example:
for (int i = 0; i < n; i++) {
arr[i] = i + 1; // assign some values to the array
elements
}
for (int i = 0; i < n; i++) {
cout << arr[i] << " "; // print the array elements
}
pointer = new type; // allocate memory for one
element of type type and assign the address to
pointer
pointer = new type[size]; // allocate memory for
an array of size elements of type type and
assign the address to pointer
delete pointer; // free the memory pointed by pointer
(one element)
delete[] pointer; // free the memory pointed by pointer
(an array)
12. DYNAMIC MEMORY ALLOCATION
Advantages: Some advantages of using dynamic memory allocation are:
It allows us to create data structures with variable sizes, such as linked lists, trees, graphs, etc.
It can improve the efficiency and performance of a program by avoiding unused or wasted
memory.
It can avoid memory overflow errors by allocating memory only when needed.
Disadvantages: Some disadvantages of using dynamic memory allocation are:
It adds complexity and overhead to a program, as we need to manage the memory manually
and check for errors.
It can cause memory leaks or errors if we forget to free the memory or use invalid pointers.
It can fragment the memory heap, making it harder to find contiguous blocks of memory.
13. RELATIONSHIP BETWEEN ARRAYS AND POINTERS
The value of an array name is the address of the first element in the array
The value of a pointer variable is an address
If the pointer points to the same data type as the array element then the pointer and
array name can be used interchangeably (almost).
14. POINTERS AND CONST IN C++
Pointers are variables that store the address of another variable or object in memory.
const is a keyword that indicates that a variable or object is constant, meaning its value cannot be
changed after initialization.
Pointers and const can be used together in C++ to create different types of pointers that have
different levels of constness.
Suppose we have two variables, a and b, of type int, and we want to create pointers to them. We
can use the following declarations:
15. POINTERS AND CONST IN C++
Now we can use these pointers to access or modify the values of a and b, depending on the type of
pointer. For example:
16. PASSING POINTERS TO FUNCTIONS
Pass-by-reference with pointer parameters
We can use pointers and the dereference operator to achieve pass-by- reference
The function parameter is a pointer
The actual parameter can be a pointer or address of a variable
void double_data(int *int_ptr);
void double_data(int *int_ptr) {
*int_ptr *= 2;
}
17. POTENTIAL POINTER PITFALLS
Uninitialized pointers
Dangling Pointers
Not checking if new failed to allocate memory
Leaking memory