Lab Lecture 1
Lab Lecture 1
Recap Pointers
K2
Memory Representation
• For example
o int num = 5;
• How is it stored in memory?
K2
Memory Representation
• For example
o int num = 5;
• How is it stored in memory?
num
5
1000
K2
Memory Representation
• For example
o int num = 5;
• How is it stored in memory?
K2
Memory Representation
• For example
o int num = 5;
• How is it stored in memory?
K2
Memory Representation
• For example
o int num = 5;
• How is it stored in memory?
• What is the binary value of 5?
num
5
1000
K2
Memory Representation
• For example
o int num = 5;
• How is it stored in memory?
• What is the binary value of 5? - (101)2
• int is 4 bytes (32 bits) in gcc/g++
num
5
1000
K2
Another Memory Representation
• For example
o int num = 5;
• How is it stored in memory?
• What is the binary value of 5? - (101)2
• int is 4 bytes (32 bits) in gcc/g++
num
5
0000 0000 1000
0000 0000 num
0000 0000
0000 0101
K2
Another Memory Representation
• For example
num
o int num = 5;
5
• How is it stored in memory?
• What is the binary value of 5? - (101)2 1000
K2
Questions?
Pointers
• What does a pointer do?
K1
Pointers
• What does a pointer do?
o Points to a memory address and sets an off-set based on the datatype of the pointer
variable.
K1
Pointers
• What does a pointer do?
o Points to a memory address and sets an off-set based on the datatype of the pointer
variable.
• What does that mean?
K1
Pointers
• What does a pointer do?
o Points to a memory address and sets an off-set based on the datatype of the pointer
variable.
• What does that mean?
K1
Pointers
• What does a pointer do?
o Points to a memory address and sets an off-set based on the datatype of the pointer
variable.
• What does that mean?
o Stores an address of another variable (Red)
o Tells how much bytes/words the target variable takes (Green)
K2
Pointers
• Syntax
o datatype *identifier_name;
K1
Pointers
• Syntax
o datatype *identifier_name;
• Example
o int *ptr;
• The empty box stores address of other variables
ptr
1000
K2
Pointers
• Syntax
o datatype *identifier_name;
• Example
o int *ptr;
o int num = 5;
ptr num
5
1000 1008
K2
Pointers
• Syntax
o datatype *identifier_name;
• Example
o int *ptr;
o int num = 5;
o ptr = &num ptr num
1008 5
1000 1008
K2
Pointers
• Syntax
o datatype *identifier_name;
• Example
o int *ptr;
o int num = 5;
o ptr = &num ptr num
• What do you get for? 1008 5
o ptr
1000 1008
o *ptr
o &ptr
K2
Pointers
• Syntax
o datatype *identifier_name;
• Example
o int *ptr;
o int num = 5;
o ptr = &num ptr num
• What do you get for? 1008 5
o ptr - 1008
1000 1008
o *ptr
o &ptr
K2
Pointers
• Syntax
o datatype *identifier_name;
• Example
o int *ptr;
o int num = 5;
o ptr = &num ptr num
• What do you get for? 1008 5
o ptr - 1008
1000 1008
o *ptr -> *(1008) -> 5
o &ptr
K2
Pointers
• Syntax
o datatype *identifier_name;
• Example
o int *ptr;
o int num = 5;
o ptr = &num ptr num
• What do you get for? 1008 5
o ptr - 1008
1000 1008
o *ptr -> *(1008) -> 5
o &ptr - 1000
K2
Pointers
• Syntax
o datatype *identifier_name;
• Example
o int *ptr;
o int num = 5;
o ptr = &num ptr num
• What do you get for? 1008 5
o ptr - 1008
1000 1008
o *ptr -> *(1008) -> 5
o &ptr – 1000
• Try the same for "num" (Homework)
K2
Questions?
Pointers
• Memory Allocation and Deallocation
o Static
o Dynamic
K1
Pointers
• Memory Allocation and Deallocation
o Static – Normal Variable
o Dynamic – malloc, calloc, realloc, free
K1
Pointers
• Memory Allocation and Deallocation
o Static – Normal Variable
Memory Management – Automatically by the OS
o Dynamic – malloc, calloc, realloc, free
K2
Pointers
• Memory Allocation and Deallocation
o Static – Normal Variable
Memory Management – Automatically by the OS
o Dynamic – malloc, calloc, realloc, free
§ Memory Management – Manually by the Programmer or Program Termination
K2
Pointers
• Memory Allocation and Deallocation
o Static – Normal Variable
Memory Management – Automatically by the OS
o Dynamic – malloc, calloc, realloc, free
§ Memory Management – Manually by the Programmer or Program Termination
• malloc Syntax:
o pointer_identifier = (cast_type*) malloc(number_of_bytes);
K1
Pointers
• Example: Normal Example: malloc
• int num = 5; • int *ptr;
• ptr = (int*)malloc(sizeof(int));
• *ptr = 5;
K2
Pointers
• Example: Normal Example: malloc
• int num = 5; • int *ptr;
• ptr = (int*)malloc(sizeof(int));
• *ptr = 5;
num
5
1000
K2
Pointers
• Example: Normal Example: malloc
• int num = 5; • int *ptr;
• ptr = (int*)malloc(sizeof(int));
• *ptr = 5;
num ptr
5 Stores only address
1000 1000
K2
Pointers
• Example: Normal Example: malloc
• int num = 5; • int *ptr;
• ptr = (int*)malloc(sizeof(int));
• *ptr = 5;
num ptr
5 2000
1000 1000 2000
K2
Pointers
• Example: Normal Example: malloc
• int num = 5; • int *ptr;
• ptr = (int*)malloc(sizeof(int));
• *ptr = 5;
num ptr
5 2000 5
1000 1000 2000
K2
Questions?
Header File Creation
• Hands-on - Hello World
K3
Lab Questions
1. Write a C++ program to find the sum of 'n' integers using only pointers. Maintain proper boundary
conditions and follow coding best practices.
2. Write a C++ program that has functions to calculate the sum, difference, product and difference of two
integer numbers. Maintain proper boundary conditions and follow coding best practices. Do the following
after that,
o Convert the file into a header file. Move it to the current working directory or global location of include
files.
o Include the newly created header file into a new C++ program
o Write a menu driven program in the new C++ program with the following menu,
1. Set
2. Add
3. Subtract
4. Multiply
5. Divide
6. Exit K4
o The set option gets and stores two integers from the user. Other options performs the corresponding
operations on the two set integers and displays the result.
Summary
• Lab Goals
• Memory Representation
• Another Memory Representation
• Pointers
• Header File Creation
• Lab Questions
References
1. Deitel, Harvey M., and Paul J. Deitel. C How to program: with an introduction to
C++, Eighth Edition, Pearson Education, 2018.
THANK YOU