the address space continued . software tools and systems...

6
2017-01-29 1 CSCB09: Software Tools and Systems Programming Bianca Schroeder [email protected] IC 460 2 The address space continued .. 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 (Heap) Stack Unused space 0 Logical address 2 32 -1 Space for dynamically allocated datastructures Read-only data: string literals ROData 3 What do you think about C so far ? Disappointed about the lack of support for strings, arrays, etc. ? Turns out you also have to do much of memory management manually . Your ride with Python: - Cruise control - Seat heating - Cup holders - Automatic transmission Your ride with C: - Performance and speed - But, no amenities - And it only comes with a stickshift 4 The address space continued .. 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 (Heap) Stack Unused space 0 Logical address 2 32 -1 Space for dynamically allocated datastructures Read-only data: string literals ROData

Upload: dinhthien

Post on 10-Jun-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The address space continued . Software Tools and Systems ...bianca/cscb09w17/posted_lectures/week4/Week… · Software Tools and Systems Programming ... IC 460 2 The address space

2017-01-29

1

CSCB09: Software Tools and Systems Programming

Bianca Schroeder [email protected]

IC 460 2

The address space continued ..

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 (Heap)

Stack

Unused space

0 Logical address

232 -1

Space for dynamically allocated datastructures

Read-only data: string literals

ROData

3

What do you think about C so far …? –  Disappointed about the lack of support for strings, arrays,

etc…. ? –  Turns out you also have to do much of memory management

manually ….

Your ride with Python: -  Cruise control -  Seat heating -  Cup holders -  Automatic transmission

Your ride with C: -  Performance and speed -  But, no amenities … -  And it only comes with a stickshift …

4

The address space continued ..

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 (Heap)

Stack

Unused space

0 Logical address

232 -1

Space for dynamically allocated datastructures

Read-only data: string literals

ROData

Page 2: The address space continued . Software Tools and Systems ...bianca/cscb09w17/posted_lectures/week4/Week… · Software Tools and Systems Programming ... IC 460 2 The address space

2017-01-29

2

5

Arrays

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

x[0]!

x[1]!

x[2]!

x[3]!

x[4]!

0x88681140 0x88681144 0x88681148 0x8868114c 0x88681150

•  Remember from last time: •  In many ways an array name can be treated like a pointer •  E.g.: *x == x[0]!•  E.g.: void x(int *a) is same as void x(int a[])!

•  So are there differences between arrays and pointers?

0x88681154 p!

6

Differences between pointers and arrays

•  For a pointer variable (e.g. p in the example) space is reserved to store an address.

•  For an array, space is reserved to store array contents, but not the array address.

x[0]!

x[1]!

x[2]!

x[3]!

x[4]!

0x88681140 0x88681144 0x88681148 0x8868114c 0x88681150 0x88681154 p!

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

7

Differences between pointers and arrays (I)

•  You can do p = 0x88681144;!•  But the following is illegal: x = 0x88681144;!•  There is no space allocated for x to hold an

address !

x[0]!

x[1]!

x[2]!

x[3]!

x[4]!

0x88681140 0x88681144 0x88681148 0x8868114c 0x88681150 0x88681154 p!

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

8

Differences between pointers and arrays (II)

•  Inside main: sizeof(p) != sizeof(x)!

!•  BUT, inside void func(int x[]):!

sizeof(x) == size of an address!

x[0]!

x[1]!

x[2]!

x[3]!

x[4]!

0x88681140 0x88681144 0x88681148 0x8868114c 0x88681150 0x88681154 p!

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

Size of an address (4 or 8 bytes)

5 * size of an int (typically 20 bytes)

Page 3: The address space continued . Software Tools and Systems ...bianca/cscb09w17/posted_lectures/week4/Week… · Software Tools and Systems Programming ... IC 460 2 The address space

2017-01-29

3

9

Differences between char pointers and char arrays (I)

! !char a[] = “array”;!! !char *p = “pointer”;!

•  As for all arrays, space is reserved to store contents of a, but not the array address.

•  Can assign new value to p, but not to a.

a = p; // Illegal!!p = a; // OK! !

10

Differences between char pointers and char arrays (II)

! !char a[] = “array”;!! !char *p = “pointer”;!

•  “pointer” is stored in read-only memory.

No other space is reserved for p,except to store a memory address.

p[0] = ‘z’; // Illegal!!a[0] = ‘z’; // OK! !

11

With all this in mind try Q1..

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 (Heap)

Stack

Unused space

0 Logical address

232 -1

Space for dynamically allocated datastructures

Read-only data: string literals

ROData

12

(I)

Page 4: The address space continued . Software Tools and Systems ...bianca/cscb09w17/posted_lectures/week4/Week… · Software Tools and Systems Programming ... IC 460 2 The address space

2017-01-29

4

13

(I)

14

(II)

15

(II)

16

(II)

Page 5: The address space continued . Software Tools and Systems ...bianca/cscb09w17/posted_lectures/week4/Week… · Software Tools and Systems Programming ... IC 460 2 The address space

2017-01-29

5

17

(III)

18

(III)

19

(IV)

20

(IV)

Page 6: The address space continued . Software Tools and Systems ...bianca/cscb09w17/posted_lectures/week4/Week… · Software Tools and Systems Programming ... IC 460 2 The address space

2017-01-29

6

21

(V)

22

(V)

23

And off to the second page of the worksheet…