cscb09: announcemen and systems · 2017-01-21 · 2017-01-20 1 cscb09: software tools and systems...

6
2017-01-20 1 CSCB09: Software Tools and Systems Programming Bianca Schroeder [email protected] IC 460 Announcement I needed to move my office hour today to 11am. I’ll be attending an event at 3pm in IC130 for students interesting in applying for subject PoSts in CS, math, stats. – If you know such people tell them to come. 3 Memory model The memory for a process (a running program) is called its address space Memory is just a sequence of bytes A memory location (a byte) is identified by an address. Code Static Data Dynamic Data Unused Logical Address Space Stack 0 2 32 -1 Logical address 4 The address space Space for global variables and variables declared as static Space for variables created in function calls: a function’s parameters and a function’s local variables Code Static data Dynamic data Stack Unused space 0 Logical address 2 32 -1 Space for dynamically allocated datastructures

Upload: others

Post on 30-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCB09: Announcemen and Systems · 2017-01-21 · 2017-01-20 1 CSCB09: Software Tools and Systems Programming Bianca Schroeder bianca@cs.toronto.edu IC 460 Announcemen • I needed

2017-01-20

1

CSCB09: Software Tools and Systems Programming

Bianca Schroeder [email protected]

IC 460

Announcement

•  I needed to move my office hour today to 11am.

•  I’ll be attending an event at 3pm in IC130 for students interesting in applying for subject PoSts in CS, math, stats. –  If you know such people tell them to come.

3

Memory model

•  The memory for a process (a running program) is called its address space

•  Memory is just a sequence of bytes

•  A memory location (a byte) is identified by an address.

Code

Static Data

Dynamic Data

Unused Logical Address Space

Stack

0

232 -1

Logical address

4

The address space

Space for global variables and variables declared as static

Space for variables created in function calls: a function’s parameters and a function’s local variables

Code

Static data

Dynamic data

Stack

Unused space

0 Logical address

232 -1

Space for dynamically allocated datastructures

Page 2: CSCB09: Announcemen and Systems · 2017-01-21 · 2017-01-20 1 CSCB09: Software Tools and Systems Programming Bianca Schroeder bianca@cs.toronto.edu IC 460 Announcemen • I needed

2017-01-20

2

5

An example 0x

int x = 10;!int y;! !int f(int p, int q) {! int j = 5;! return p * q + j;!}! !int main() {! ! int i = x;! y = f(i, x);! return 0;!}!

5

Code

10

10

Unused space

??

0x8049430 x 0x8049434 y

0xffff3a3c i

10 10 5 0xffff3a30 j

0xffff3a38 q 0xffff3a34 p

Static data

Stack

main

f

6

Arrays

int x[5];!for (i = 0; i < 5; i++) {! x[i] = i*i;!}!

x[0]!

x[1]!

x[2]!

x[3]!

x[4]!?

0x88681140 0x88681144 0x88681148 0x8868114c 0x88681150 0x88681154

•  Arrays in C are a contiguous chunk of memory that contain a list of items of the same type.

•  If an array of ints contains 10 ints, then the array is 40 bytes. There is nothing extra.

•  In particular, the size of the array is not stored with the array. There is no runtime checking.

•  So you can access x[99999999]!•  What does that mean?

7

Pointer Arithmetic •  The array access operator [ ] is really only a shorthand

for pointer arithmetic + dereference •  These are equivalent in C:

a[i] == *(a + i)!

•  So for a[9999999], the program will happily try to access contents at address *(a+9999999)!

l  Behaviour of exceeding array bounds is “undefined” à  program might appear to work à  program might crash (segmentation fault) à  program might do something apparently random

Why do pointers need a type, e.g. int* or char*?

Page 3: CSCB09: Announcemen and Systems · 2017-01-21 · 2017-01-20 1 CSCB09: Software Tools and Systems Programming Bianca Schroeder bianca@cs.toronto.edu IC 460 Announcemen • I needed

2017-01-20

3

9

int i = 0;!int a[4] = {0, 1, 2, 3};!int *p; !p = a;! !for(i = 0; i < 4; i++) {! printf("%d\n", *(p + i)); !}!

a[0]

a[1]

a[2]

a[3]

•  Hint: Why does adding 1 to p move it to the next spot for an int, when an int is 4 bytes?

0

3

2

1

(*p) ==

*(p + 1) ==

*(p + 2) ==

*(p + 3) ==

0x1140

0x1144

0x1148

0x114c

Why do pointers need a type, e.g. int* or char*?

10

a[0]

a[1]

a[2]

a[3]

0

3

2

1

(*p) ==

*(p + 1) ==

*(p + 2) ==

*(p + 3) ==

0x1140

0x1144

0x1148

0x114c

•  Pointer arithmetic respects the type of the pointer.

•  E.g., !int *p;!*(p+1) …;!!really adds 4 to p

•  C knows the size of what is being pointed at from the type of the pointer.

Why do pointers need a type, e.g. int* or char*?

11

Questions about pointers before we start with page 1 of

the Pointer Worksheet?

12

Page 4: CSCB09: Announcemen and Systems · 2017-01-21 · 2017-01-20 1 CSCB09: Software Tools and Systems Programming Bianca Schroeder bianca@cs.toronto.edu IC 460 Announcemen • I needed

2017-01-20

4

13

Let’s see why lie() on the worksheet did not work as

expected?

14

Passing function arguments 0x

int x = 10;!int y;! !int f(int p, int q) {! int j = 5;! p = 5;! return p * q + j;!}! !int main() {! ! int i = x;! y = f(i, i);! return 0;!}!

14

Code

10

10

Unused space

??

0x8049430 x 0x8049434 y

0xffff3a3c i

10 10 5 0xffff3a30 j

0xffff3a38 q 0xffff3a34 p

Static data

Stack

main

f

If f() were to modify p or q, will that change the value of main’s int i?

15

Passing function arguments 0x

int x = 10;!int y;! !int f(int p, int q) {! int j = 5;! x = 5;! return p * q + j;!}! !int main() {! ! int i = x;! y = f(i, i);! return 0;!}!

15

Code

10

10

Unused space

??

0x8049430 x 0x8049434 y

0xffff3a3c i

10 10 5 0xffff3a30 j

0xffff3a38 q 0xffff3a34 p

Static data

Stack

main

f

If f()were to modify x or y would this change be permanent? 16

Passing function arguments 0x

int x = 10;!int y;! !void f(int *p, int q) {! *p = 5;!}! !int main() {! ! int i = x;! f(&i, i);! return 0;!}!

16

Code

10

10

Unused space

??

0x8049430 x 0x8049434 y

0xffff3a3c i

10 0xffff3a38 q 0xffff3a34 p

Static data

Stack

main

f 0xffff3a3b When calling f():

Page 5: CSCB09: Announcemen and Systems · 2017-01-21 · 2017-01-20 1 CSCB09: Software Tools and Systems Programming Bianca Schroeder bianca@cs.toronto.edu IC 460 Announcemen • I needed

2017-01-20

5

17

Passing function arguments 0x

int x = 10;!int y;! !void f(int *p, int q) {! *p = 5;!}! !int main() {! ! int i = x;! f(&i, i);! return 0;!}!

17

Code

10

5

Unused space

??

0x8049430 x 0x8049434 y

0xffff3a3c i

10 0xffff3a38 q 0xffff3a34 p

Static data

Stack

main

f 0xffff3a3b After f() returns:

18

With this in mind try to work through questions 3-6 on the

Pointer Worksheet!

19

Before we look at the Command-line Worksheet, a few words about strings..

20

Strings •  Strings are not a built-in data type •  A string is an array of chars terminated by null character (‘\0’).

•  Initializing a string: char course_name[8] = {‘c’,’s’,’c’,’b’,’0’,’9’,’h’,’\0’};!

!

Or more conveniently: char course_name[8] = “cscb09h”;!!

Now you can do all the usual array operations, e.g. course_name[3] = ‘z’;!

•  Other ways to initialize the string: char course_name[80] = “cscb09h”;! char course_name[2] = “cscb09h”; // OUCH!!!!

!

!!!

Page 6: CSCB09: Announcemen and Systems · 2017-01-21 · 2017-01-20 1 CSCB09: Software Tools and Systems Programming Bianca Schroeder bianca@cs.toronto.edu IC 460 Announcemen • I needed

2017-01-20

6

21

Strings

After initializing a string: char course_name[8] = {‘c’,’s’,’c’,’b’,’0’,’9’,’h’,’\0’};!

!

What is the type of course_name?

char *!!!

What type would an array of strings be? char **!

!

!

So what is argv in int main(int argc, char **argv)? An array of strings.

!!!

22

Now try to work through Q2-5 on the command-line argument

sheet!

That’s it for today!