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 145 Lecture 9 – Loops: for and while
Agenda exam coming up! for loop problems digitizing a circle spreadsheet fill for (INIT; TEST; UPDATE) { ACTION } INIT while (TEST) { ACTION UPDATE } while loop problems summing an indefinite number of numbers checking for repeated words decomposing a comma-separated list prompting for an actual file TODO Read chapter 3 (mostly discussed already) […]
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 145 Lecture 8 – Loops
Agenda for loops acrostic spelunking display approximating a circle Code ForFun.java package preexam1; public class ForFun { public static void main(String[] args) { for (int i = 0; i < 3; i = i + 1) { System.out.println(“Bye, Patrick!”); } int sum = 0; for (int i = 1; i <= 100; i = i […]
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 145 Lecture 7 – Careful coding
Agenda relational operators software disaster careful coding pseudocode build in small pieces test pieces in isolation document some problems to test area of ring Code Utilities.java package preexam1; public class Utilities { // i = get area of inside // o = get area of outside // area = o – i public static double […]
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; […]
CS 145 Lecture 6 – Methods 2
Agenda what does this do? the separation between caller and callee building a snowman method chaining What does this do? Code . <h4>Area.java</h4> <pre class=”code”> package preexam1; public class Area { public static void main(String[] args) { double r1 = 5; // java.awt.Toolkit.getDefaultToolkit().beep(); double area = getArea(8); System.out.println(r1); } public static double getArea(double radius) { […]