MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte...

27
MODEL OF MEMORY C++ ARRAYS Problem Solving with Computers-I

Transcript of MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte...

Page 1: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

MODEL OF MEMORYC++ ARRAYS

Problem Solving with Computers-I

Page 2: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Memory and C++ programs2

“The overwhelming majority of program bugs and computer crashes stem from problems of memory access... Such memory-related problems are also notoriously difficult to debug. Yet the role that memory plays in C and C++ programming is a subject often overlooked…. Most professional programmers learn about memory entirely through experience of the trouble it causes.”

…. Frantisek Franek(Memory as a programming concept)

Page 3: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Model of memory• Sequence of adjacent cells

• Each cell has 1-byte stored in it• Each cell has an address

(memory location)

012345678910

char x = 1;int y = 4;char tmp = x;x = y;y = tmp;

Page 4: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Array motivation• Write a program to record the midterm scores of 10 students in CS16, by

asking the user to input each score. Then print out each of the recorded scores

Page 5: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

C++ Arrays A C++ array is a list of elements that share the same name, have the same data type and are located adjacent to each other in memory

scores

scores10 20 30 40 50 60 70 8010

20 30 40 50 60 70 80

Page 6: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

What is the memory location of each element?

int scores[5]={10, 20, 30, 40, 50};

If the starting location of the array is 0x200, what is memory location of element at index 2?A. 0x201B. 0x202C. 0x204D. 0x208

scores 10 20 30 40 50

Page 7: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Declaring C++ arrays

int scores[5]; // declares a 5-element integer array//declare a 5-element char array

? ? ? ? ?

Page 8: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Declaring and initializing, accessing elements

// Declare a 5-element integer array and fill it with values

int scores[5]={10, 20, 30, 40, 50};

Page 9: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Exercise: Reassign each value to 60

int scores[]={20,10,50}; // declare and initialize//Access each element and reassign its value to 60

scores[0] scores[1] scores[2]

20 10 50

Page 10: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Exercise: Increment each element by 10

int scores[]={20,10,50}; // declare and initialize//Increment each element by 10

Page 11: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

C++ 11 range based for loop

int scores[]={20,10,50}; // declare and initialize//Print each element using a range based for loop

Page 12: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Most common array pitfall- out of bound access

int arr[]={20,10,50}; // declare and initializefor(int i=0; i<=3; i++)

scores[i] = scores[i]+10;

Page 13: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Tracing code involving arrays

int arr[]={1,2,3};int tmp = arr[0];arr[0] = arr[2];

arr[2] = tmp;

arr[0] arr[1] arr[2]1 2 3

arr[0] arr[1] arr[2]

Choose the resulting array after the code is executed

2 1 3arr[0] arr[1] arr[2]

3 2 1arr[0] arr[1] arr[2]

A.

B.

C.

D. None of the above

Page 14: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Arrays – motivating example

DEMO: Write a program to store 10 scores and calculate the average of the 10 scores.

Page 15: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

POINTERSProblem Solving with Computers-I

Page 16: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

How comfortable do you feel with using github?A. Very comfortable in the context of labs; I have a basic understanding of how

git worksB. I know how to use it but I have no idea how git worksC. I don’t feel comfortable using itD. I am completely lost

Page 17: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Swap function

Page 18: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

• Pointer: A variable that contains the address of another variable• Declaration: type * pointer_name;

18

int *p;

How do we initialize a pointer?

Pointers

Page 19: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

19

int *p;int y; p y

100 112

How to make a pointer point to something

To access the location of a variable, use the address operator ‘&’

Page 20: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

int *p, y; p ? y ?

p ? y 3

p112 y

3

100 112

100 112

100 112

p points to y

How to make a pointer point to something

Page 21: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

21

p

112y

3

100 112

Pointer Diagrams: Diagrams that show the relationship between pointers and pointees

Pointer: p Pointee: y

Page 22: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

You can change the value of a variable using a pointer !

Use dereference * operator to left of pointer name

22

int *p, y;y = 3;p = &y;

*p = 5;

Page 23: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Tracing code involving pointers

Q: Which of the following pointer diagrams best represents the outcome of the above code?

23

int *p, x=10;p = &x;*p = *p + 1;

A.10x

B.x

C. Neither, the code is incorrect

11

p p

Page 24: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

py 3

24

Change the value of y directly.

Change the value of y indirectly (via pointer p).

Two ways of changing the value of a variable

Page 25: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Pointer assignment and pointer arithmetic: Trace the code

25

int x=10, y=20;

int *p1 = &x, *p2 = &y;

p2 = p1;

int **p3;

p3 = &p2;

Page 26: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Pointer assignment

Q: Which of the following pointer diagrams best represents the outcome of the above code?

26

int *p1, *p2, x;p1 = &x;p2 = p1;

A.

xB.

x

C. Neither, the code is incorrect

Page 27: MODEL OF MEMORY C++ ARRAYSModel of memory •Sequence of adjacent cells •Each cell has 1-byte stored in it •Each cell has an address (memory location) 0 1 2 3 4 5 6 7 8 9 10 char

Swap function 2