class: center, middle # COMP 321 Lab 1 ## Alan Cox and Scott Rixner --- layout: true --- # Remember COMP 222! * C * GitHub and GitHub Classroom * CLEAR9 Review the COMP 222 information on [GitHub and CLEAR](https://www.clear.rice.edu/comp222/html/laboratories/lab01/) Link your NetID to your GitHub account in GitHub Classroom Remove your `.vscode-server` directory on CLEAR9 to remove any remnants of your COMP 222 environment --- # Lab Expectations * Show up to lab! * Complete the exercises * Collaborate: work with each other * Ask questions --- # Lab Extra Credit * Completing the lab material is **strongly** encouraged * Directly useful in the homework assignments * May be on the exams * Attendance will be taken at every lab * If you attend all of the labs, you will receive extra credit * Grade bump if you are within 0.25 points of the next letter grade * 3 "pre-excused" absences for any reason (sick, traveling, etc.) * No need to contact the instructors to use these absences --- # Code Style * Code style is important * Always use a consistent style * Follow the COMP 321
Style Guide
-- First and foremost rule of good coding style: > The style of your code, e.g., its indentation, should match the style of the surrounding code. --- # Today: Dynamic Arrays in C Build an array abstraction in C that: * Can grow and shrink dynamically * Can store elements of any type -- Underlying storage should: * Be allocated on the heap in contiguous memory * Double in size when full * Half in size when less than 1/4 full * Be deallocated when the array is freed --- # Dynamic Arrays in C .mb-0[ Dynamic array interface: ] ``` typedef struct DynamicArray DynamicArray; DynamicArray *dynarray_create(size_t element_size); int dynarray_get(DynamicArray *array, size_t index, void *destination); int dynarray_set(DynamicArray *array, size_t index, void *element); int dynarray_append(DynamicArray *array, void *element); int dynarray_pop(DynamicArray *array); size_t dynarray_len(DynamicArray *array); void dynarray_free(DynamicArray *array); ``` -- .mb-0[ DynamicArray struct: ] ``` struct DynamicArray { size_t element_size; // size of each element in bytes size_t current_size; // current number of elements in the array size_t capacity; // current capacity (in number of elements) of the array void *data; // pointer to the array's data // (of size element_size * capacity) }; ``` --- # Useful Functions Read the man pages for usage information * `malloc(3)`, `realloc(3)`, and `free(3)` * `memcpy(3)` and `memmove(3)` --- class: center, middle # Get Started!