CS 330 Lecture 18 – Callbacks and XML
Agenda we need a chairperson OpenGL game loops in GLUT hello, XML push parsing with Expat a GDB primer TODO Read the first three pages of http://www.ibm.com/developerworks/xml/tutorials/xmlintro/index.html. Code torus.c #include <stdio.h> #include <stdlib.h> #include <GL/glut.h> /* ————————————————————————- */ int x_angle = 0; int y_angle = 0; /* ————————————————————————- */ void draw_callback() { glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glRotatef(x_angle, 1.0f, […]
CS 330 Lecture 17 – Higher-order functions
Agenda program this the strategy pattern enums in C function pointers GLUT callbacks Expat callbacks TODO http://www.json.org/xml.html http://www.codinghorror.com/blog/2008/05/xml-the-angle-bracket-tax.html http://c2.com/cgi/wiki?XmlSucks Program This With a neighbor, write a short C function that chooses a random number between 0 and 100 and lets a computer player guess until it picks the right answer. Code guess_a_number.c #include <stdio.h> #include […]
CS 330 Lecture 16 – Assembly
Agenda a little peek at GNU Assembly code transformation to preprocessed to assembly to object code to linked executable variables? no, registers %ebp and %esp on call and return local variables %eax for return values passing arguments to functions arrays pointers conditional statements loops Code ls.c #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <sys/types.h> int […]
CS 330 Lecture 15 – Malloc
Agenda preprocessor macros memory leaks adjusting an allocation with realloc how malloc works, kinda p1 = malloc(4 * sizeof(int)) p2 = malloc(5 * sizeof(int)) p3 = malloc(6 * sizeof(int)) free(p2) p4 = malloc(2 * sizeof(int)) TODO For more background on malloc, browse http://www.gnu.org/software/libc/manual/html_node/Memory-Allocation.html#Memory-Allocation. Read http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax. Code makefile EXES = first arrays months oob strings test_stack all: $(EXES) […]
CS 330 Lecture 14 – Stack API
Agenda why malloc? a stack API evaluating postfix expressions for each token if token is number, push it on operand stack if token is operator, pop two operands, push result back on pop stack to get the value separate compilation command-line arguments atoi TODO Read http://www.joelonsoftware.com/articles/fog0000000319.html. Read http://gee.cs.oswego.edu/dl/html/malloc.html. Code makefile EXES = first arrays months oob strings […]
CS 330 Lecture 13 – Strings and structs
Agenda array out-of-bounds strings null-terminated char arrays strlen strcat strcpy strdup strcmp structs header files a stack API evaluating postfix expressions for each token if token is number, push it on operand stack if token is operator, pop two operands, push result back on pop stack to get the value Code makefile EXES = first […]
CS 330 Homework 3 – due before Tuesday, March 6
See the PDF.
CS 330 Lecture 12 – Arrays and malloc
Agenda a C makefile arrays strings dynamic allocation a stack API Code makefile EXES = first arrays months all: $(EXES) $(EXES): %: %.c gcc -o $@ -Wall -std=c99 $< clean: rm -f $(EXES) arrays.c #include <stdio.h> #include <stdlib.h> int average(int nums[], int len) { int sum = 0; for (int i = 0; i < […]
CS 330 Lecture 11 – Hi, C
Agenda quiz review Call subclass in Flop the birth of C compiling printf sizeof functions and forward declarations scanf getting addresses with & pointers Code first.c #include <stdio.h> // —————————————————————————- // Function declarations // —————————————————————————- void clamp(int *number); void clampByValue(int number); // —————————————————————————- // Function implementations // —————————————————————————- int main(int argc, char **argv) { double […]
CS 330 Lecture 10 – Translating to an AST
Agenda adding functions to Flop storing parse results in an AST quiz Code Flop.g grammar Flop; @header { import java.util.HashMap; } @members { HashMap<String, Integer> memory; HashMap<String, Expression> functions; public abstract class Expression { public abstract int evaluate(); } public class Literal extends Expression { private int value; public Literal(int value) { this.value = value; […]