0% found this document useful (0 votes)
2 views31 pages

Lecture 3

The document discusses references and pointers in C++, highlighting their definitions, initialization, and differences. References provide an alternative name for an object and are used primarily for pass-by-reference in functions, while pointers hold the address of another object and allow for dynamic memory allocation. It also addresses the importance of pointers, pointer arithmetic, and potential issues such as memory leaks and dangling pointers.

Uploaded by

Adnaan Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views31 pages

Lecture 3

The document discusses references and pointers in C++, highlighting their definitions, initialization, and differences. References provide an alternative name for an object and are used primarily for pass-by-reference in functions, while pointers hold the address of another object and allow for dynamic memory allocation. It also addresses the importance of pointers, pointer arithmetic, and potential issues such as memory leaks and dangling pointers.

Uploaded by

Adnaan Syed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Programming

Paradigms in C++
CSCI 3142

By

Priyanka Samanta
Reference in C++
A reference defines an alternative names for an object. A reference type
“refers to” another object. Once a reference is initialized with a variable, either
the variable name or the reference name may be used to refer to the variable.

▪ You cannot have NULL references. You must always be able to assume that
a reference is connected to a legitimate piece of storage.
▪ Once a reference is initialized to an object, it cannot be changed to refer to
another object. (java is more flexible)
▪ A reference must be initialized when it is created.
Reference definition
&d, where d is the name of the reference

int i = 17;
int& r = i; //r is an integer reference initialized to I

int &r1; //Error: a reference must be initialized

Note: When we define a reference, instead of copying the initializer’s value, we bind the
reference to it’s initializer.
Why we need a reference?
The main use of references is acting as function formal parameters to support
pass-by-reference. In a reference variable is passed into a function, the
function works on the original copy (instead of a clone copy in pass-by-value).
Changes inside the function are reflected outside the function.

Important: After a reference has been defined, all operations on that reference are actually
operations on the object to which the reference is bound!

Code link: https://replit.com/@SamantaPriyanka/Pointer-and-


reference#reference.cpp
Reference
definitions
Reference to const
Unlike an ordinary reference, a reference to const cannot be used to change
the object to which the reference is bound.
Initialization and references to const
We can bind a reference to const to a non constant object, a literal, or
a more general expression
Initialization and references to const

Const reference are allowed to bind to an object of different type


Pointer in C++
A pointer is a compound type that “points to” another object. They are
used for indirect access to other objects

Reference vs Pointer
• You cannot have NULL references. You must always be able to assume that a reference
is connected to a legitimate piece of storage. A pointer is an object of its own right.
• Once a reference is initialized to an object, it cannot be changed to refer to another
object. Pointers can be pointed to another object at any time.
• A reference must be initialized when it is created. Pointers can be initialized at any
time.
Pointer definition
*d, where d is the name of the pointer
What do pointers hold?
A pointer holds the address of another object
We get the address of an object using address of operator (&)
Pointer initialization & dereferencing

Code link: https://replit.com/@SamantaPriyanka/Pointer-and-reference#pointer_basic.cpp


Examples of Pointers
Importance of pointers in C++
Why do we need to create a pointer just to assign or change value?
- pointer can be used to pass values by reference to a function and can
return multiple values from the function
- pointers can be used in combination of arrays
- For dynamic memory allocation
- A pointer of an object of a base class can be used to access an object of a
derived class
- Concept of smart pointer
Pointers and const
• Like references, we can also define pointers that point to either const or
non-const types.
• It is also possible to declare pointers that can access the pointed value to
read it, but not to modify it.
• We can store the address of a const object only in a pointer to const
Pointers and const
Const Pointer
• Unlike reference, pointers are objects
• Like any other constant object, pointers can also be constant itself
• A const pointer must be initialized, and once initialized its value (i.e the
address it holds) can not be changed
Const Pointer
Pointer and array

Code link: https://replit.com/@SamantaPriyanka/Pointer-and-


reference#pointer_array.cpp
Pointer arithmetic
• To conduct arithmetical operations on pointers is a little different than to
conduct them on regular integer types.
• Only addition and subtraction operations are allowed
• Both addition and subtraction have a slightly different behavior with
pointers, according to the size of the data type to which they point.
• For example: char always has a size of 1 byte, short is generally larger than
that, and int and long are even larger; the exact size of these being
dependent on the system. For example, let's imagine that in a given system,
char takes 1 byte, short takes 2 bytes, int takes 4 byte and long takes 8.

Code link: https://replit.com/@SamantaPriyanka/Pointer-and-


reference#pointer_arithmetic.cpp
Pointer arithmetic
• Pointer Increment : p++
• Pointer Decrement : p—
• Constant Addition: p+k
• Constant Subtraction: p-k
• Subtraction of 2 pointers: l=q-p
Pointer arithmetic addition
Prefix and postfix operators
*p++ // same as *(p++): Read the data and then move the pointer
(*p)++ // Read the data, and then increment the data

*++p // same as *(++p): move the pointer, and then read the data
++*p // same as ++(*p): Read the data, and increment the value it points to
Pointers and heap memory
and dynamic array
• Dynamic array size can be changed unlike fixed array
• Dynamic array is created using “new” keyword
• Dynamic array is created in heap memory which is dynamically allocated using run
time, not at compile time
• Fixed size array created in stack and gets automatically deleted once goes out of
scope
• Dynamic array does not get deleted and will be available as long as the program is
running
• Using pointer, we can access dynamic array created in heap (unlike Java)
• Can cause memory leak, if not handled properly
C++ dynamic array
Code link: https://replit.com/@SamantaPriyanka/Pointer-and-
reference#dynamicArray_pointer.cpp
Multidimensional dynamic array

Code link: https://replit.com/@SamantaPriyanka/Pointer-and-


reference#multi_dynamicArray_pointer.cpp
C++ code
Problem with pointers
• Pointers are very dangerous when not using properly. Due to pointer, our
system may crash due to run time error.
• Error types:
➢ Uninitialized pointer
➢ Memory leak
➢ Dangling pointer
• int *p //bad practice
• int x=10;
int *p=&x; //good practice
• p=(int*) 0x5638; //we can directly assign address to pointer if we are sure
this address belongs to our program
Problem with pointers
Memory leak:
int *p=new int[5];
delete[]p;
p=nullptr;
Dangling pointer:
int main(){
int *p=new int[5];
fun(p);
cout<<*p;
}
Here p is a dangling pointer.

You might also like