Anda di halaman 1dari 29

Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Topics Covered

- Passing Arguments to Functions by Reference


and address of (&) operator
- Arrays (1D)
- Brief Introduction to Memory Addressing and
Pointers

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Passing Arguments to Functions by Reference

- Address of (&) operator


- Aliasing/AKAs

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Addressing Memory Locations (Bytes/Words)


RAM, 1 KW*
1024
1023
1022

CPU 1021
1020

RAM

4
I/O Devices
3
2
1
0

* 1 Word = 32 bits = 04 bytes

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Reading Memory Addresses


ith Run

1024
1023
1024
1022
1023 1st Run
1021
1022
1020
1021
1020 4
3
4
2
3
1
2
0
1
0
Ghulam Ishaq Khan Institute of Science and Technology, Topi
Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Passing Arguments to Functions by Reference

- Address of (&) operator


- Aliasing/AKAs

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

5 ?

2
1
0

The variable value and v are two local variables with different scope.
If we print the addresses of both variables, they are different.

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

The variable value and v are aliases and if we print the addresses of
both variables, they are same.

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Arrays in C++

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Arrays in C++
• An array allows you to store and work with multiple values of the
same data type.
• int hours[6]; //6 locations each of 4 bytes

• hours[0] = 100; // writing


• cout << hours[1]; // 1 is index or subscript or position indicator

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Arrays in C++ (Size of Allocated Memory)

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Out-of-bound Memory Accesses

This often leads to hard-to-find bugs resulting in segmentation faults.

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Memory Addressing
Arrays, Pointers, Strings

Dr. Sajid Anwar

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Reading Memory Addresses


1023

1022
n 100
1021

1020

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Array Addressing
int attendance[10];
1023

… What is the first element in the array ? attendance[0]

What is the address of the first element


in the array ? &attendance[0]
. attendance
.
.

4 function1(…) function2(…)
3
2
1
0
Take the starting address (base address) and the
total size (10) and do the required computation.

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Memory Addressing

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Pointer Variables
1023
1022
1021
1020

x 25 4
3
2
1
0

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Strings = Arrays of Characters

“Hello !”
“My Registration Number is 2017000”

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

• Strings
• Arrays of characters
• All strings end with null ('\0')
• Examples:
char string1[] = "hello";
char string1[] = { 'h', 'e', 'l', 'l', 'o',
'\0’ };
• Subscripting is the same as for a normal array
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'

• Input from keyboard


char string2[ 10 ];
cin >> string2;
• Takes user input
• Side effect: if too much text entered, data written beyond array

Ghulam Ishaq Khan Institute of Science and Technology, Topi


1 // Fig. 4_12: fig04_12.cpp
Inputted strings are
2 // Treating character arrays as strings
separated by whitespace
3 #include <iostream>
characters. "there"
4 stayed in the buffer.
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 int main()
10 {
11 char string1[ 20 ], string2[] = "string literal";
12
13 cout << "Enter a string: ";
14 cin >> string1;
15 cout << "string1 is: " << string1
16 << "\nstring2 is: " << string2
17 << "\nstring1 with spaces between characters is:\n";
Notice how string elements are
18
referenced like arrays.
19 for ( int i = 0; string1[ i ] != '\0'; i++ )
20 cout << string1[ i ] << ' ';
21
Enter a string: Hello there
22 cin >> string1; // reads "there"
string1 is: Hello
23 cout << "\nstring1 is: " << string1 << endl; string2 is: string literal
24 string1 with spaces between characters is:
25 cout << endl; H e l l o
26 return 0; string1 is: there
27 }
Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Sorting Arrays
• Sorting data
• Important computing application
• Virtually every organization must sort some data
• Massive amounts must be sorted
• Bubble sort (sinking sort)
• Several passes through the array
• Successive pairs of elements are compared
• If increasing order (or identical), no change
• If decreasing order, elements exchanged
• Repeat these steps for every element

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Sorting Arrays
• Example:
• Original: 3 4 2 6 7
• Pass 1: 3 2 4 6 7
• Pass 2: 2 3 4 6 7
• Small elements "bubble" to the top

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

Case Study: Computing Mean, Median and Mode


Using Arrays (Next Class)
• Mean
• Average
• Median
• Number in middle of sorted list
• 1, 2, 3, 4, 5 (3 is median)
• Mode
• Number that occurs most often
• 1, 1, 1, 2, 3, 3, 4, 5 (1 is mode)

Ghulam Ishaq Khan Institute of Science and Technology, Topi


Lecture 24: Arrays and Memory Addressing in C++ CS 101: Introduction to Computing

References
Dietal and Dietal : How to Program C++
3rd Edition

Starting out with C++ Early Objects


7th Edition

Ghulam Ishaq Khan Institute of Science and Technology, Topi