Lecture 8: Generics

CS107 Topic 4: How can we use our knowledge of memory and data representation to write code that works with any data type?

Table of contents:


This is best explained via examples:

Example

memcpy

So what happens if we have overlapping regions? We use...

memmove

Pitfalls

With great power comes great responsibility

Swap ends

Example

Pointer arithmetic

(char *)arr + (num_elems * size_of_each_elem) 

This makes sense. We also ne

ed to cast to a char * first since a char is one byte and we can do pointer arithmetic with them (unlike void *'s).

Example

Recap