- UNIX and C - GNU Programming Chapter 1, 2 - K&R pretty much entire book Week 1: UNIX ------------------------- - OS Tasks - manage devices - handles file organization - process manager - Process - key idea is that of a process, has a lifetime - FILE: - LOAD: - EXECUTE: - EXIT: - Layout - many things in UNIX are tree based - process structure - file system structure - Shell - user interacts with OS through a shell process - command intepreter that looks for and executes executable files - special characters >, <, &, | - Devel Environment - 1 edit code - 2 compile code - 3 execute / debug - 4 goto 1 Week 2: C Fundamentals ------------------------- - C program made up of: 1 - preprocessor directives 2 - definitions (prototypes/structs) 3 - variable declarations 4 - function declarations (main, foo) 5 - expressions 6 - control structures 1 - Preprocessor Directives - start with # - #define FOO 10 - #include - there are others 2 - Function prototypes / Struct definitions - user defined reserved names - must define functions and structs before they're used - function prototype - ( , , etc); - struct define - struct { ; ; etc }; 3 - Variable Declarations - defn of variable: placeholder for data that can change during program execution - variables must be declared - memory reserved before variables used - variables are typed - determines how much memory is required - determines what ops can be performed - types can be converted - implicitly using C promotion scheme (smaller get promoted to larger) - explicitly using casting - variables can be operated upon using operators - binary math operators: +, -, *, /, =, ... - binary conditional operators: &&, ||, !, !=, == - unary operators: ++, --, * (deref), & (addr of) - no tertiary operators!! - a == b == c IS LIKE SAYING a == (b == c) which is not what you expect 4 - Function Declarations - function declarations must match prototype definition - when function is called, values of params copied to new variables that exist for function duration main() { int a, b; a = 4; b = foo(10, &a); } int foo(int one, int *two) { return(one - *two); } - can only return one value 5 - Expressions - expressions result in a number - expressions can contain - function calls - variables - operators on variables - constants - variables, operators, functions composed to form expressions - in C, FALSE=0, TRUE=nonzero - order of operations imposed by C on expression evaluation - math expressions usually make assignment at the end - a = - conditional expressions usually result in truth (1) or falsehood (0) - a = b && c; - a = b || (c && d); 6 - Control Structures - control the flow of your program, branching and looping - branching is making decisions based on expression results if (expr) then X else Y - looping is repeating a process until expression is true while(expr is true) do X - know syntax for - if/else, if/else if/else - for, while Week 3: Arrays, Pointers, UNIX process model, Dynamic Memory, and Structures -------------------------------------------------- - key ideas: - only one variable copied/operated on at a time - pointers are just like other variables, have additional operations - defn: pointers store the address of an allocated region in memory - syntax: * int *iptr; int a; - iptr = &a; - usefulness of pointers - 1.) can access memory (variables) outside current function scope - 2.) can use to abstractly 'return' or 'pass' more than one variable to function - 3.) can use to access (index) more than one variable at a time - 4.) required for C dynamic memory - arrays satisfy a subset of pointer usefulness, arrays are ALMOST pointers themselves - arrays allow for convenient indexing of ddim problems - arrays are constant, cannot grow or shrink or be reassigned to other memory UNIX Process Model ------------------------- -------------------------------- |a |b | stack frame (main) | |-------------------------------| |i |p | stack frame (foo) | |-------------------------------| | | | | | | | | | | | | |-------------------------------| | heap | |-------------------------------| | data seg | |-------------------------------| | | | text seg | | | | | --------------------------------- - Text Seg contains all compiled machine instructions - Data Seg contains all static and global variables - stack grows down, each time function encountered, space for function code and space for variables is created in a stackframe and then executed - each function gets a new stack frame - when function is complete, stack frame memory returned to OS Dynamic Memory in C ------------------------- - what if program requires some dynamic memory during program execution? - borrow memory from the heap - use malloc, give it the # of bytes to borrow - malloc(sizeof(int) * 32); - malloc returns an addr where borrowed memory starts - must cast returntype, otherwise couldn't index - it's up to use to return the memory to the system! - free(addr); - careful not to lose the addr returned by malloc, called a memory leak C Structs ----------------------- - used for variable encapsulation - can store multiple pieces of information in one user defined type - same rules as normal variables, including pointer types - have some special operators . for accessing encapsulated variables from declared struct -> for accessing encaps vars from struct pointer - = and == work, but must be extremely careful in the case where have structs with pointers inside them Recursion ----------------------------- - one reason stack model exists, and progs don't run out of text seg, is because funcs can call themselves - each time function is called, new stack frame is allocated - recursive functions are usually of the form float func(float in) { if () return(1); else return(in op func(in op change)) } - most recursive procedure can be solved with loops and memory, sometimes more elegant to use recursion Data Structures ------------------------------ - usually use exactly as much memory as currently required (grow and shrink) - have properties that make them appropriate for some data set - Linked lists - globally accessible head pointer - elements minimally contain data and pointer to other element - can simulate stacks (add to head, remove from head) or queues (use tail pointer, new node are added to tail, nodes removed from head) - last node points to NULL