- C: Everything is pass-by-value - Simple example of params vvv void swap(int a, int b) { int tmp = a; a = b; b = tmp; } int a = 1; int b = 2; swap(a, b); - Simple example of return vals typedef struct { double x; double y; } Point; Point create(double a, double b) { Point p1 = {.x = a, .y = b} return p1; } - What about arrays? Possible not to pass by pointer? - Remember not to pass back pointers into this stack frame!!! Point* create(double a, double b) { Point p1 = {.x = a, .y = b} return &p1; //NOT GOOD USE MALLOC INSTEAD } - Java: Everything pass-by-value - All primitives pass by value (ie ints, boolean) - All objects are *pointers* passed by value public Point swapX(Point a, Point b) { double tmp = a.x; a.x = b.x; b.x = tmp; return a; } Point* swapX(Point* a, Point* b) { double tmp = a->x; a->x = b->x; b->x = tmp; return a; } - C++: Teaser, pass by reference is a thing! Use & instead of * - HW2: What is it? - Implement a 2D array library...from scratch: Must support: - Generic objects to store - Way to edit the locations of objects in array - Must have no leaks - Some design decisions: - How to implement array itself? 1st: Single pointer - allocation: a rows, b columns int* arr = (int*) malloc(a * b * sizeof(int)); - Usage: a[i * num_cols + j] 2nd: Pointers to pointers - allocation: a rows, b columns int** arr = (int**) malloc(a * sizeof(int*)); for (int i = 0; i < b; i++) arr[i] = (int*) malloc(b * sizeof(int)); - Usage: arr[i][j]; 3rd: Double pointer to all rows - allocation: a rows, b columns arr = (int **)malloc(a * sizeof(int*)); arr[0] = (int *)malloc(b * a * sizeof(int)); for(i = 0; i < r; i++) arr[i] = (*arr + c * i); - Usage: arr[i][j] - How to implement generics? - void *!! - What functions to implement? - Get and Set are a must! - Think about what you might need for candy crush - How to handle errors? - Pass back as return value? - Pass back as return parameter? - Creating something like errno? (suggest only) - Also an exercise about how to read Documentation - What is serialization / deserialization? - A way to store data that your program makes in a file - A way to read in data from a file - Jansson JSON loader library - Number, Boolean, String, Object, Array - Example input: { "rows": 2, "columns":3, "data":[0,1,2,3,4,5] <--- these can be anything (client binary can expect int's) (library must support anything!) } - How does Jansson work? json_load_file() returns root json object memory management? reference counters use json_decref() to indicate finished with object - Functions you might want to look at: json_is_object(), json_object_get() json_is_integer(), json_integer_value() json_is_array(), json_array_get()