STRUCTURED TYPES: typedef struct vs struct name alias vs. non-alias use dot (.) operator to access members of the structure like get method.
‘BStree_node.someMember = setSomeValue’. the members are like the public methods in a java class use assignment (=) operator to copy the contents
of one struct to another struct of the same type. emptyStruct = nonEmptyStruct populates NEStruct memory allocation of structures its members are
stored consecutively as declared. can have an array of structs where each entry is of type struct.pointers to structures must be of the same type as
structure. For (*p).someMember, the () are necessary because . precedes *. pointer points to first byte of the structure / first byte of first declared variable
use arrow (->) operator to access the members through the pointer. ptr->someMember == (*ptr).someMember == ptdTo.someMember pointer to struct
memory allocation must be same size as struct. struct thing *ptrToThing = (struct thing*) malloc(sizeof(struct thing)). struct thing *arrThingPtr = (struct
thing*) malloc (arrSize * sizeof(struct thing)) freeing pointers frees the memory allocated using malloc, mem block available for use again. values stored in
members still there if not yet overwritten or cleared, but can’t access through the pointer. dangling pointer can be fixed by ptr = NULL after free(ptr).
memcpy (dest = src) == ( memcpy(&dest, &src, sizeof(thing)) ). Just pt to same addy, not making a deep copy of src content into dest strdup does malloc
and strcpy in one step. char *strdup (const char *s). can be used for duplicating. const char *original = “hi :3”; string char *duplicate = strdup(original).
duplicate = “hi :3” passing a structure (not ptr) as a parameter func creates local copy and mods that, returns a copy. if ptr is passed (not struct itself),
function modifies and returns the struct directly DEBUGGING IN C: Prototype : gdb [programName] [core | processID ]. gcc -g -o my_program
my_program.c to compile into debugger. gdb commands:b(reak) fileName:mySpot or b(reak) mySpot - set a breakpoint at a lineNum or function mySpot.
b 42 == break 42 == break myProgram mthd == b myProgram methd. watch variable - keep track of the value of variable, stop when changed. Use c or
continue to keep going after it stops at a point. bt or where - show call stack before program crashed. print variable | expression - print the value of
variable or expression. display variable | expression - show value of variable or expression each time the program stops. X someAddress - examines
memory at some given address. r(un) [arglist] - run one or a series of different arguments as param to your function main call. run Hello there ; Hello there
is input parameter. n(ext) vs s(tep) Both go to next line in the program. Next skips over function calls if encountered. Step goes into the function calls if
encountered. l(ist) fileName:mySpot - list the code written in mySpot line or mySpot function. f(rame) stackSpot - select and print a stack frame of a call
stack in memory. h(elp) commandName - show info about a command. q(uit) - quite debugger. x/FMT ADDRESS - show specified bytes at given address.
FMT: Repeat count, format letter, and size letter. ADDRESS: Expression for the memory address to examine. Common Format Letters: x: Hexadecimal. t:
Binary. d: Decimal. u: Unsigned decimal. f: Float. c: Char. s: String. Common Size Letters:. b: Byte (8 bits). h: Halfword (16 bits, 2 bytes). w: Word (32 bits,
4 bytes). g: Giant (64 bits, 8 bytes). x/1tb &integerVar -> 0b00101010. x/1tg &integerVar -> 0b000000000…000000101010 JOBS AND PROCESSES IN
UNIX someTask & to run task in background, can still use shell for other tasks. Bg task will have a process ID and job number Con: when done and print
output, it'll randomly pop up on your screen xterm& open new window, but leave the current shell window still available to use Ctrl-C : interrupts and
terminates current process in foreground usually. NOT put in bg. To interrupt and term process in bg, do fg %procID first to put in foreground Ctrl-Z: stop a
process in fg and put it in background while stopped. Can continue the process in bg (using bg %procID) or put it back in fg to continue. Use just fg to put
most recent process put in bg to fg. fg %processNum to bring it to fg from bd, immediate resume jobs command shows your processes and whether they
are running or stopped kill %jobNum; kill -9 %jobNum or kill -KILL %jobNum if wont die kill -STOP %jobNum will only stop the process and it can be
continued like usual ps lists the current processes ps -l ; list processes in long list format ps -u loginID tells you about loginID’s processes if you can
access other users kill pid - kills the process standing for pid (NOT job). TERM signal will be sent to the process pid. kill –9 or kill –KILL will send the KILL
signal Use man kill to find out more signals Process vs job Job is a collection of processes. Process is the execution of a program ulimit option changes
limits of file size writing and processes able to be run at once ulimit -a ; prints all limits ulimit -n fileSizeLimit ; set limit of file size in bytes ulimit -u procLimit
; set limit of number of processes available (regardless of state - running, paused, stopped, unless killed) ADVANCED POINTER TOPICS Calling the
pointer name gives ptr[0], the first thing it points to in an array/structure. In a 2dArr[2][3], using the name of the array will give 2dArr[0] storing 3 items.
Using the name of a pointer to 2dArr gives 2dArr[0] *(*(a+i)+j) is a[i][j] A uses pointer arithmetic moving a[0] to a[i]. After dereferencing a[i] it gives that
whole row (array), pointing to a[i][0]. In that dereferenced row arr, adding j will give a[i][j]. Dereference to get value of a[i][j]. ary->[3]->[3] = 6 is illegal, (
ary[3][3] = 6 ) == ( *( *( ary + 3) + 3) = 6 ) is legal 2d array as a parameter Need at least col num in paramUsing const in pointer parameters To indicate
we are not changing what is being passed as a param, in the function Ptr to functions int (*compare)(int, int) = funcName; compare is a pointer to a
method taking in (int,int) and this pointer returns an int. Int varName = (*compare)(int, int) qsort(void *firstElem, size_t numElems, size_t elemSizeByte, int
(*compar)(const void *, const void *) Compare func pointer is within q sort. qsort(my_books, 1000, sizeof(book), compare_price) ADTs IN C Create ADT in
three different files. File to hold type definitions and constant declarations (ProgramTypes.h). File to hold prototypes of public functions in ADT interface
(ProgramInterface.c). File to hold implementations of public and private functions (ProgramImplmnts.c). Sometimes, the two header files can be combined
Interface file #include “ProgramTypes.h”, then proceed to define prototypes Implementation file “Include “ProgramInterface.h”, then proceed to implement
the functions Avoiding multiple overlapping definition errors #ifndef FileName_H, #define FileName_H, …typedef…, #endif. Use a different identifier with
#ifndef for each .h file MAKEFILE sample: sample.o my_stat.o // dependencies, sample is target output; cc –o sample sample.o my_stat.o // commands;
sample.o: sample.c my_stat.h; cc –c sample.c; my_stat.o: my_stat.c my_stat.h; cc –c my_stat.c; clean; rm –f sample *.o core. Must be stored in same
directory make clean removes all generated files.export: StackImplementation.oStackImplementation.o: StackImplementation.c \; ../Include/StackTypes.h
\ ; ../Include/StackInterface.h; gcc -I../Include -c StackImplementation.c; This block defines the rules for building the StackImplementation.o object file.
StackImplementation.o depends on StackImplementation.c, ../Include/StackTypes.h, and ../Include/StackInterface.h.. gcc line compiles
StackImplementation.c into an object file (-c flag). The -I../Include flag tells the compiler to look for header files in the ../Include directory. -c vs. -o vs. -I
gcc -c myfile.c ; compiles myfile.c into object file named myfile.o gcc -o name myfile.c ; compiles myfile.c into an executable named name gcc -l../someDir
; specifies additional directories to look for .h files in. it is possible to specify a list of directories separated by commas with -I. by using -I (capital i), we can
avoid having to put copies of a .h file in the subdirectories for every .c file that depends on the .h file () in makefiles When unix commands are placed
inside (), a new subprocess is created. cmds in () are executed as part of that subprocess (cd ../Stack; make export) Subprocess switches to Stack dir
and executes make ->Upon termination, parent process resumes in original dir -> Like function calls in a function call Macros ; name = text string ;
creates a macro named name whose value is text string Calls to $(name) or ${name} are replaced by text string Example CC = gcc; HDIR = ../Include;
INCPATH = -I$(HDIR); DEPH = $(HDIR)/StackTypes.h $(HDIR)/StackInterface.h; SOURCE = StackImplementation; export: $(SOURCE).o;
$(SOURCE).o: $(SOURCE).c $(DEPH) $(CC) $(INCPATH) -c $(SOURCE).c; print: ; lpr $(SOURCE).c ; clean: ; rm -f *.o. Notation target: commands;
(base case:) If target is not a file, execute commands; target: dep1 dep2 ... depn; commands; (general case:) for i from 1 to n; if depi is a target, make
depi; If target is not a file, execute commands; else if at least one of depi is not a file, execute commands; else if at least one of depi is newer than target,
execute commands SHELL ENVIRONMENT $VAR to get value of shell variables Two kinds of shell variables - Environment variables : specific to shell
and the programs invoked from the shell. export myVar = “some string”. Makes myVar available to all other processes in the shell even in other sessions
Regular Shell variables : specific to curr shell session or script. myOtherVar = “some other string”. Makes myOtherVar available to curr shell script or
about:blank 1/2