CPS 210 22F Some topics and skills to focus for Midterm 1 on 9/30. Base types in C - How big are the base types on x64? - Signed vs. unsigned types - How does a C program allocate storage to store values/variables? (three ways) - What segment is my variable in? Stack, global, heap? (Based on how it is allocated.) - 32-bit vs. 64-bit machine: how are the sizes of the types different? - Sizeof() - Portable code C procedures - Procedures/functions: arguments, parameters, return type - ASM: use of stack and registers to implement procedure call/return and arguments/parameters - main() - argc and argv and their use - Return type/value from main - Importance of explicitly returning an integer from main (e.g., for gradescope) Representations: - Unsigned integers, char/ASCII encodings as byte-sized integers - Convert between binary or hex and decimal - Given an ASCII table, translate between alphanumeric and encoding in decimal or hex - Encoding values in compact bit fields as in the playing cards or storeproj examples - Role of shift and mask and or operators to get/set values in bit fields Representations: floats. Out of scope for midterm 1. Representations: signed integers (2's complement) - Given a hex value, determine what bits are set. - For a signed value in hex, determine if the value is positive or negative. - For a signed value in hex, determine if it is even/odd or divisible by 2, 4, 8 - Given values in hex, which value is <>, including absolute value (magnitude). - We won't ask you to convert signed values or do arithmetic on values, but know you can do it Assignments - C: = vs == - lvalues and rvalues for = (assignment) - ASM: Understand how assignments are implemented in x64 instructions - ASM: How are size/type of values reflected in these instructions? - ASM: Sign-extended assignments - ASM how x64 operates on different sizes: how are the compiled instructions different between long and int? Boolean operators and their meanings in C - Conditionals, && and || and ! - Interpret a zero value as false - Interpret any nonzero value as true - Bitwise operators: <<, >>, &, |, ~ - Apply bitwise logical operations to hex values. - Sign-extended "arithmetic" shifts - Understand how these operators correspond to x64 instructions (L08 slides) - How do size/type of values affect the choice of instruction variants? C: Structs and arrays - Defining struct types - Typedef - Defining/allocating arrays - Allocating structs and arrays of structs - How big? (minimum size in bytes) - Array indexing with subscripts 0..n-1: what byte offset? - C: accessing member fields of a struct with . - C: accessing member fields of a struct through a pointer with -> - C: memory layout of struct types. What are the offsets of member fields of a struct? - C: memory layout of single-dimension arrays: what are the offsets of subscripted elements a[i]? - ASM: LEAQ and addressing modes - ASM: accessing member fields of a struct: how is it implemented in x64 instructions? - ASM: accessing an array element a[i]: how is it implemented in x64 instructions? - ASM: accessing member fields of element a[i]: how is it implemented in x64 instructions? C strings as arrays of chars - Allocating space for strings at runtime - Initializing string variables, e.g., char[] s = "hope"; - Allocating space for string constants/literals (e.g., "hope") - Null terminator: space for null terminator, presence of null terminator in string literal - Sizeof a char* vs. size (length) of a string: strlen or strnlen - Null terminator: strlen and strlen don't count it - String equality vs. string pointer equality: strcmp and strncmp - Copy strings: strcpy and strncpy - What is different about the n variants of strlen, strcmp, strcpy? strnlen, strncmp, strncpy? - Why would I use the n variants? What are the risks of using strlen, strcmp, strcpy? - How to know what value of n to use for the n variants strnlen, strncmp, strncpy? Give an example. - Variable-size array arguments/parameters: []. How passed? - Printing/accessing string arrays as ints or longs: endianness - x64 is little-endian C: Pointers - Defining pointer types and allocating pointer variables with with * - Obtaining pointer values with & - Accessing values referenced by a pointer: dereference operator * - ASM: how is pointer dereference implemented in x64 instructions? - Arrays vs,: pointers: "the same", "interchangeable" - Array variables allocate space for referenced value, but pointer variables do not - Obtain a pointer to an array and access elements of the array using pointer arithmetic - Obtain a string pointer from an array of chars - Pointer-to-pointer: ** types: concept of a handle. Why are handles useful as arguments/parameters? - Pointer-to-pointer: char **argv vs. char* argv[] - Type casting - Array subscripting and array overflow/underflow: what will happen? - Wild pointers - NULL pointers - What causes programs to crash? What is a segfault? - What happens on a segfault? - void*: why is it useful? ASM: key high-level concepts and x64 examples: - How instructions reference registers for operands - The processor clock and fetch-execute cycle - How instructions encode/specify the types (and sizes) of their operands. - The role of sized MOV instructions to copy data between registers - Loads and stores: accessing memory with sized moves in x64 - Addressing modes for loads and stores and their role for answering ASM questions above - Code runtime: roughly how long does it take to execute some sequence of instructions? - Code runtime: What does the cost depend on? - Code runtime: mportance of memory references (loads and stores) C printf and scanf: - Concept of standard runtime library - stdin and stdout - stdlib, stdio header files vs. linking of library procedures - Format specifiers as string literals with % - Specifiers: know the most useful ones, but we won't ask you - Allocating space to receive data from scanf - Errors from scanf: how to check - Passing values (e.g., strings) to printf - We won't ask about file IO: fopen, fread, fwrite Heap - C: malloc and free - We won't ask about calloc and realloc - The heap manager in the standard library: header files vs. linking - Pointer to heap block: how to get it, how to use it, how to free it - Need for type casting (vs. Java "new") - Need for sizeof (vs. Java new) - What happens if the heap fills up? - The heap manager "contract" as a key part of the heap abstraction vs. API - Why does the contract not promise to zero returned heap blocks? - What if you violate the contract? - Memory leaks: what could go wrong? How to find/fix them? - Dangling references: what could go wrong? - Getting it wrong: garbage values and segfaults - What if you free() a heap block twice? - What if you free() on a pointer that is not a valid heap block? - How does the heap manager detect such errors? See also: exam policies on the course web. https://courses.cs.duke.edu/fall22/compsci210d/exams.html Highlights: - No screeens - Everything closed - One page of your own notes (two sides) - You may bring the x64 reference sheet Ed Posts! Key content copied here for your convenience. https://edstem.org/us/courses/25403/discussion/1804756 - The September 30 midterm covers through lecture L08, and not lecture L09 as originally announced.. - The in-class exams shall test only material that is covered/cited in lectures or exercised in labs/projects. - For exams, you can bring a copy of the x86-64 assembly reference sheet on the resources page, and in addition you may bring one page of notes. https://edstem.org/us/courses/25403/discussion/1844997 - You should know about the requirement for address alignment (below). - We will ask you about sizes on the exam, but ignore any padding (below). - Alignment: Any address of a source or destination operand of an instruction should be aligned on a boundary for the size specified in the instruction op, i.e., the address is divisible by that size. - Padding: Alignment considerations may cause the compiler to insert padding (empty bytes) in data layouts as discussed here and in class. And not just for structs, but between variables. But one of you got me to promise yesterday that I would not ask anything related to padding on the exam. https://edstem.org/us/courses/25403/discussion/1828131 Valgrind does more than check for memory leaks! It also checks if your code runs off the end of a heap block. Looking at people's code, this appears to be the more common error. In particular: Use the str functions. Understand how str functions handle the null terminators for strings. Make sure your code handles strings and their null terminators properly. Be sure to allocate space you need for that null terminator. Know what is the meaning of the value returned by strlen()