Personal SE Computer Memory Addresses C Pointers Computer Memory - - PowerPoint PPT Presentation

personal se
SMART_READER_LITE
LIVE PREVIEW

Personal SE Computer Memory Addresses C Pointers Computer Memory - - PowerPoint PPT Presentation

Personal SE Computer Memory Addresses C Pointers Computer Memory Organization Memory is a bucket of bytes . Computer Memory Organization Memory is a bucket of bytes. Each byte is 8 bits wide. Computer Memory Organization Memory


slide-1
SLIDE 1

Personal SE

Computer Memory Addresses C Pointers

slide-2
SLIDE 2

Computer Memory Organization

◼ Memory is a bucket of bytes.

slide-3
SLIDE 3

Computer Memory Organization

◼ Memory is a bucket of bytes.

– Each byte is 8 bits wide.

slide-4
SLIDE 4

Computer Memory Organization

◼ Memory is a bucket of bytes.

– Each byte is 8 bits wide. – Question: How many distinct values can a byte of data hold?

slide-5
SLIDE 5

Computer Memory Organization

◼ Memory is a bucket of bytes.

– Each byte is 8 bits wide. – Question: How many distinct values can a byte of data hold? – Bytes can be combined into larger units:

▪ Half-words (shorts) 16 bits 65,536 combinations ▪ Words (ints) 32 bits  4  109  4 billion ▪ Double words (long) 64 bits  16  1018  16 quadrillion

slide-6
SLIDE 6

Computer Memory Organization

◼ Memory is a bucket of bytes.

– Each byte is 8 bits wide. – Question: How many distinct values can a byte of data hold? – Bytes can be combined into larger units:

▪ Half-words (shorts) 16 bits 65,536 combinations ▪ Words (ints) 32 bits  4  109  4 billion ▪ Double words (long) 64 bits  16  1018  16 quadrillion

◼ The bucket is actually an array of bytes:

slide-7
SLIDE 7

Computer Memory Organization

◼ Memory is a bucket of bytes.

– Each byte is 8 bits wide. – Question: How many distinct values can a byte of data hold? – Bytes can be combined into larger units:

▪ Half-words (shorts) 16 bits 65,536 combinations ▪ Words (ints) 32 bits  4  109  4 billion ▪ Double words (long) 64 bits  16  1018  16 quadrillion

◼ The bucket is actually an array of bytes:

– Think of it as an array named memory.

slide-8
SLIDE 8

Computer Memory Organization

◼ Memory is a bucket of bytes.

– Each byte is 8 bits wide. – Question: How many distinct values can a byte of data hold? – Bytes can be combined into larger units:

▪ Half-words (shorts) 16 bits 65,536 combinations ▪ Words (ints) 32 bits  4  109  4 billion ▪ Double words (long) 64 bits  16  1018  16 quadrillion

◼ The bucket is actually an array of bytes:

– Think of it as an array named memory. – Then memory[ a ] is the byte at index / location / address a.

slide-9
SLIDE 9

Computer Memory Organization

◼ Memory is a bucket of bytes.

– Each byte is 8 bits wide. – Question: How many distinct values can a byte of data hold? – Bytes can be combined into larger units:

▪ Half-words (shorts) 16 bits 65,536 combinations ▪ Words (ints) 32 bits  4  109  4 billion ▪ Double words (long) 64 bits  16  1018  16 quadrillion

◼ The bucket is actually an array of bytes:

– Think of it as an array named memory. – Then memory[ a ] is the byte at index / location / address a. – Normally the addresses run from 0 to some maximum.

slide-10
SLIDE 10

Pictorially … N byte Memory

N - 1 N - 1

Either way (horizontal or vertical) is fine. The key is that memory is logically an array

slide-11
SLIDE 11

What's In a Number?

◼ What does the hexadecimal number 0x4A6F65 mean?

slide-12
SLIDE 12

What's In a Number?

◼ What does the hexadecimal number 0x4A6F65 mean? ◼ Possibilities:

– It could be the decimal number 4,878,181 – It could be the string "Joe" 'J' = 0x4A, 'o' = 0x6F, 'e' = 0x65 – It could be the address of the 4,878,181st byte in memory – It could be an instruction to, say, increment (op code = 0x4A) a location (address = 0x6F65) by 1

slide-13
SLIDE 13

What's In a Number?

◼ What does the hexadecimal number 0x4A6F65 mean? ◼ Possibilities:

– It could be the decimal number 4,878,181 – It could be the string "Joe" 'J' = 0x4A, 'o' = 0x6F, 'e' = 0x65 – It could be the address of the 4,878,181st byte in memory – It could be an instruction to, say, increment (op code = 0x4A) a location (address = 0x6F65) by 1

◼ How do we know??????

slide-14
SLIDE 14

What's In a Number?

◼ What does the hexadecimal number 0x4A6F65 mean? ◼ Possibilities:

– It could be the decimal number 4,878,181 – It could be the string "Joe" 'J' = 0x4A, 'o' = 0x6F, 'e' = 0x65 – It could be the address of the 4,878,181st byte in memory – It could be an instruction to, say, increment (op code = 0x4A) a location (address = 0x6F65) by 1

◼ How do we know?????? ◼ We don't until we use it!

slide-15
SLIDE 15

What's In a Number?

◼ What does the hexadecimal number 0x4A6F65 mean? ◼ Possibilities:

– It could be the decimal number 4,878,181 – It could be the string "Joe" 'J' = 0x4A, 'o' = 0x6F, 'e' = 0x65 – It could be the address of the 4,878,181st byte in memory – It could be an instruction to, say, increment (op code = 0x4A) a location (address = 0x6F65) by 1

◼ How do we know?????? ◼ We don't until we use it!

– If we send it to a printer, it's a string. – If we use it to access memory, it's an address. – If we fetch it as an instruction, it's an instruction.

slide-16
SLIDE 16

Computer Numbers as Shape-Shifters

◼ The ability of numbers to "morph" their meaning is very

powerful.

– We can manipulate characters like numbers. – We can change instructions on the fly. – We can perform computation on addresses.

slide-17
SLIDE 17

Danger Will Robinson! Danger!

◼ The ability of numbers to "morph" their meaning is very

powerful.

– We can manipulate characters like numbers. – We can change instructions on the fly. – We can perform computation on addresses.

◼ BUT: What if we use a number other than intended:

– We get run-time errors (using an integer as an address). – We get hard-to-fix bugs (executing data as instructions). – We get weird printout (sending addresses to a printer).

slide-18
SLIDE 18

Spiderman Is A "C" Programmer

◼ The ability of numbers to "morph" their meaning is very

powerful.

– We can manipulate characters like numbers. – We can change instructions on the fly. – We can perform computation on addresses.

◼ BUT: What if we use a number other than intended:

– We get run-time errors (using an integer as an address). – We get hard-to-fix bugs (executing data as instructions). – We get weird printout (sending addresses to a printer).

With great power comes great responsibility.

slide-19
SLIDE 19

Pointers in C

◼ Consider the following two declarations:

int i ; int *ip ;

slide-20
SLIDE 20

Pointers in C

◼ Consider the following two declarations:

int i ; int *ip ;

"*" says that ip is a pointer, not an integer

slide-21
SLIDE 21

Pointers in C

◼ Consider the following two declarations:

int i ; int *ip ;

The "*" is attached to the variable, not the type

slide-22
SLIDE 22

Pointers in C

◼ Consider the following two declarations:

int i ; int *ip ;

int i, *ip ; Equivalent to these two declarations

slide-23
SLIDE 23

Pointers in C

◼ Consider the following two declarations:

int i ; int *ip ;

◼ On most systems, both allocate 32 bits for i and ip.

slide-24
SLIDE 24

Pointers in C

◼ Consider the following two declarations:

int i ; int *ip ;

◼ On most systems, both allocate 32 bits for i and ip. ◼ The difference?

– i's contents are treated as an integer – just a number. – ip's contents are treated as an address (where an integer can be found).

slide-25
SLIDE 25

Pointers in C

◼ Consider the following two declarations:

int i ; int *ip ;

◼ On most systems, both allocate 32 bits for i and ip. ◼ The difference?

– i's contents are treated as an integer.

▪ All we can manipulate is the integer value in i.

– ip's contents are treated as an address (where an integer can be found).

▪ We can manipulate the address (make it point elsewhere). ▪ We can manipulate the integer at the current address. NOTE: int* i1, i2; vs. int *i1, *i2;

slide-26
SLIDE 26

A Short Example

double x = 3.14159 ; double y = 2.71828 ; double *dp ;

NAME ADDR VALUE x 108 108 3.141 14159 59 y 116 116 2. 2.718 1828 dp dp 124 124 ????? ????? ??

slide-27
SLIDE 27

A Short Example

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ;

NAME ADDR VALUE x 108 108 3.141 14159 59 y 116 116 2. 2.718 1828 dp dp 124 124 ????? ????? ??

slide-28
SLIDE 28

A Short Example

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ;

NAME ADDR VALUE x 108 108 3.141 14159 59 y 116 116 2. 2.718 1828 dp dp 124 124 ????? ????? ?? & = "address of" The address of a variable is a pointer to the variable's type

slide-29
SLIDE 29

A Short Example – The Effect

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ;

NAME ADDR VALUE x 108 108 3.141 14159 59 y 116 116 2. 2.718 1828 dp dp 124 108 108

slide-30
SLIDE 30

A Short Example

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ;

NAME ADDR VALUE x 108 108 3.14159 y 116 116 2. 2.718 1828 dp dp 124 108 108

slide-31
SLIDE 31

A Short Example

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ;

NAME ADDR VALUE x 108 108 3.14159 y 116 116 2. 2.718 1828 dp dp 124 108 108 * = "dereference" The value the pointer addresses, not the pointer itself

slide-32
SLIDE 32

A Short Example – The Effect

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ; // same as x = x * 2.0

NAME ADDR VALUE x 108 108 6.283 28318 18 y 116 116 2. 2.718 1828 dp dp 124 108 108

slide-33
SLIDE 33

A Short Example

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ; // same as x = x * 2.0 dp = &y ;

NAME ADDR VALUE x 108 108 6.283 28318 18 y 116 116 2. 2.718 1828 dp dp 124 108 108

slide-34
SLIDE 34

A Short Example – The Effect

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ; // same as x = x * 2.0 dp = &y ;

NAME ADDR VALUE x 108 108 6.283 28318 18 y 116 116 2. 2.718 1828 dp dp 124 116 116

slide-35
SLIDE 35

A Short Example

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ; // same as x = x * 2.0 dp = &y ; *dp += x ;

NAME ADDR VALUE x 108 108 6.283 28318 18 y 116 116 2. 2.718 1828 dp dp 124 116 116

slide-36
SLIDE 36

A Short Example – The Effect

double x = 3.14159 ; double y = 2.71828 ; double *dp ; dp = &x ; x = *dp * 2.0 ; // same as x = x * 2.0 dp = &y ; *dp += x ;

NAME ADDR VALUE x 108 108 6.283 28318 18 y 116 116 9. 9.001 0146 dp dp 124 116 116

slide-37
SLIDE 37

Pointers – Reference Parameters

slide-38
SLIDE 38

Pointers – Reference Parameters

// // Sw Swap ap – the the wro wrong ng wa way vo void id sw swap( ap( gr grade_ de_en entry try x, x, gra grade de_en _entry try y ) y ) { { gr grade de_e _ent ntry y te temp p ; tem temp = p = x x ; x x = = y ; y ; y y = t = temp emp ; ret return urn ; ; }

slide-39
SLIDE 39

Pointers – Reference Parameters

// // Sw Swap ap – the the wro wrong ng wa way vo void id sw swap( ap( gra grade_ de_en entry try x, x, gra grade de_en _entry try y ) { y ) { gr grade de_e _ent ntry te temp p ; tem temp = p = x x ; x x = = y ; y ; y y = t = temp emp ; ret return urn ; ; } // Sw // Swap ap – th the rig e right wa ht way vo void id sw swap( ap( gra grade_ de_en entry try *x, *x, gra grade_e e_entr ntry *y *y ) { { gra grade_ de_ent entry tem temp ; p ; tem temp = p = *x *x ; ; *x *x = = *y *y ; * *y y = t = temp emp ; re retur urn n ; }

You would call the function using: int x = 5; int y 7; swap(x, y); You would call the function using: int x = 5; int y 7; swap(&x, &y);

slide-40
SLIDE 40

Pointers – Call by Reference

slide-41
SLIDE 41

Pointers – Call by Reference

// Array element exchange the wrong way swap( grade_list[ i ], grade_list[ j ] ) ;

slide-42
SLIDE 42

Pointers – Call by Reference

// Array element exchange the wrong way // Array element exchange the wrong way swap( grade_list[ i ], grade_list[ j ] ) ; swap( grade_list[ i ], grade_list[ j ] ) ; // Array element exchange the right way // Array element exchange the right way swap( &grade_list[ i ], &grade_list[ j ] ) ; swap( &grade_list[ i ], &grade_list[ j ] ) ;