+ - 0:00:00
Notes for current slide
Notes for next slide

Lecture 7

30 September 2024

Admin Matters
Unit 15: Pointer
Unit 16: Call-by-Reference
Unit 17: Heap
Unit 18: String

AY24/25 S1              1

PE0 Reminder

  • Tomorrow (1 October, 6 - 9 PM)

  • Report to your assigned laboratory by 6 PM

AY24/25 S1              2

Midterm Reminder

  • Next Monday (7 October, 4:30 - 6:15 PM)

  • Report to MPSH 2B by 4:30 PM

AY24/25 S1              3

Deadline Extensions

  • Quiz 7: +1 Day due to PE0

  • Exercise 3: +2 Days due to PE0 and Midterm

AY24/25 S1              4

PE1

Covers until

  • Unit 18 (today's lecture)

  • Exercise 4

AY24/25 S1              5
AY24/25 S1              6

Variable

  • A place in memory that holds one or more values

  • Every variable has a name, a type, a value, and an address

  • We can access a variable through its name or its memory address

AY24/25 S1              7

Accessing a Variable Through Its Name

a = b;

Copy the value stored in memory location named b

into the memory location named a.

AY24/25 S1              8

Address-of operator &

if x is the name of a variable, &x is its memory address.

AY24/25 S1              9

Array Decay

long a[30];
long *addr = a;

a is just &a[0].

AY24/25 S1              10

Pointer

A variable that stores a memory address (mostly of other variables)

long c;
long *addr;
addr = &c;
AY24/25 S1              11

Accessing the Address of a Variable

a = &b;

Copy the address of the memory location named b

into the memory location named a.

AY24/25 S1              12

Dereference operator *

if x is a pointer (a variable storing a memory address), *x is the content of that memory address.

not to be confused with the pointer type (e.g., long *) or multiplication (e.g., 2 * 3).

AY24/25 S1              13

Changing variable via its address

pointer

AY24/25 S1              14

Accessing a Variable Through Its Address

a = *b;

Copy the value stored in the memory location of which the address is stored in memory location named b

into the memory location named a.

AY24/25 S1              15

Accessing a Variable Through Its Address

*a = b;

Copy the value stored in the memory location named b

into the memory location of which the address is stored in memory location named a

AY24/25 S1              16

Common Bug

long *addr;
*addr = 15;
AY24/25 S1              17

During execution, your code must access only

  1. Memory on the allocated stack frames
AY24/25 S1              18

Pointer Types

  • if x as the type T, &x has the type T* (pointer to T / address of T)

  • if x as the type T*, *x has the type T

AY24/25 S1              19

Pointers must be of the same type

double p = 3.1415926;
long radius = 5;
double *addr;
addr = &p; // ok
addr = &radius; // not ok

(void * is special)

AY24/25 S1              20

Pointer arithmetic

long x = 1;
long *ptr = &x;
ptr += 1;
AY24/25 S1              21

a[i] is just another notation for *(a + i)

AY24/25 S1              22

Pointers are variables too

long x;
long *ptr = &x;
long **ptrptr = &ptr;
AY24/25 S1              23

The NULL Pointer

A special pointer value that says a pointer points to "nothing".

AY24/25 S1              24

Call by Reference

(non-array variables)

AY24/25 S1              25

Swap

void swap(long *a, long *b) {
long temp = *a;
*a = *b;
*b = temp;
}
int main() {
long a = 10;
long b = -4;
swap(&a, &b);
}
AY24/25 S1              26

Heap

AY24/25 S1              27
  • Heap is another region of memory that a program can utilize

  • Explicitly "borrows" memory space for use

  • Must "return" the memory space after use

AY24/25 S1              28

Why heap?

  • Lifetime of content stored on heap can exceed the lifetime of the function that allocates it

  • Size of heap is typically much larger than stack

AY24/25 S1              29

How to borrow: malloc or calloc

void *malloc(size_t size);
void *calloc(size_t count, size_t size);

How to return: free

void free(void *);
AY24/25 S1              30

During execution, your code must access only

  1. Memory on the allocated stack frames
  2. Memory on the heap that the code borrows but hasn't returned
AY24/25 S1              31

Type size_t

  • A unsigned integer type used for array index, amount of memory, size of types, etc.
AY24/25 S1              32

Fixed-Size vs Dynamic Arrays

  • Fixed-size arrays: size known in advanced (e.g., there are 10 digits, 12 months, etc.)

  • Dynamic arrays: size known only during execution (e.g., number of students in a course)

AY24/25 S1              33
long* cs1010_read_long_array(size_t how_many)
{
long *buffer = calloc(how_many, sizeof(long));
if (buffer == NULL) {
return NULL;
}
for (size_t i = 0; i < how_many; i += 1) {
buffer[i] = cs1010_read_long();
}
return buffer;
}
AY24/25 S1              34
size_t len = cs1010_read_size_t();
long *list = cs1010_read_long_array(len);
long max = find_max(list, len);
free(list);
AY24/25 S1              35

Common Bug 1

size_t len = cs1010_read_size_t();
long *list = cs1010_read_long_array(len);
long max = find_max(list, len);
free(list);
list[0] = 0; // use after free
AY24/25 S1              36

Common Bug 2

size_t len = cs1010_read_size_t();
long *list = malloc(sizeof(long) * len);
list = cs1010_read_long_array(len); // memory leak!
long max = find_max(list, len);
free(list);
AY24/25 S1              37
Size Known in Advanced Size Determined During Run-time
Stack Fixed-size Array VLA (stay away)
Heap stay away malloc / calloc
AY24/25 S1              38

Characters and Strings

AY24/25 S1              39

char literals

'A', 'a', '4', '0', '*', '\n', '\\'

A literal is a constant value in C program that cannot be changed (as opposed to a variable).

AY24/25 S1              40
'A' + 1 == 'B'
AY24/25 S1              41

String

  • An array of char
  • Terminated with the null character '\0'
char str[5] = { 'a', 'b', 'c', 'd', '\0' };

is the same as

char str[5] = "abcd";
AY24/25 S1              42

String Literals

Stored in read-only "text segment" in memory

char *str1 = "Hello"; // str1 points to read-only array
char str2[6] = "Hello"; // str2 points to array on stack
str1[0] = 'h'; // run-time error
str2[0] = 'h'; // ok
AY24/25 S1              43

Summary of Important Rules

  • Do not access memory space you are not allowed to.

  • Always return memory space that you borrow.

AY24/25 S1              44

Homework

  • Exercise 3
  • Problem Set 15-17
  • Diagnostic Quiz 7
AY24/25 S1              45
AY24/25 S1              46

PE0 Reminder

  • Tomorrow (1 October, 6 - 9 PM)

  • Report to your assigned laboratory by 6 PM

AY24/25 S1              2
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow