Computer
Systems

Spring 2013
course
navigation

Feb 7

style notes from Alex He.

1. malloc and casting : Why not to cast a malloc
2. declaration styles - to point or not to point typedef struct _node{ struct _node *node1; struct _node *node2; } *node; int main(){ node n; }
vs
typedef struct _node node; struct _node { node *node_ptr1; node *node_ptr2; }; int main(){ node *node_ptr; }

C tutorial, Fridays

Talk about Cory's intention to do a 2-credit C tutorial instead of this class. Could be an option for others if this material is too much right now.

homework

What do you think about this code?
int main(){ char* input; printf("Please enter a word: "); scanf("%s", &input); printf("You said '%s'.\n", &input); return 0; }
We wrote this in class, as an example of dynamic list of dynamic struct :
#include <stdio.h> #include <stdlib.h> typedef struct _point { int x; int y; } point; int main(){ // allocate a pointer to a list of pointers to points. point** ptrs; // pointer to list of point // OR allocate all the space at compile time // point data[128]; int count; int x,y; int i; printf("How many points? "); scanf("%d", &count); ptrs = malloc(sizeof(point*) * count); for (i=0; i<count; i++){ printf("%d : x y = ", i); scanf("%d %d", &x, &y); ptrs[i] = malloc(sizeof(point)); ptrs[i]->x = x; ptrs[i]->y = y; } return 0; }
---
discuss big vs little endian, and what C looks like to detect it
What does this do, and how does it work? (show_bytes is mostly from the textbook's code examples.) typedef unsigned char *byte_pointer; void show_bytes(byte_pointer start, int len) { int i; printf(" at %p : 0x ", start); for (i = 0; i < len; i++) printf(" %.2x", start[i]); printf("\n"); } int main(){ int x = 1234; int y = -10; char* buffer; char hello[] = "hello"; show_bytes(&x, 16); show_bytes("12345", 16); show_bytes(&y, 16); show_bytes(buffer, 16); show_bytes(&buffer, 16); show_bytes(hello, 16); show_bytes(&hello, 16); return 0; }
work through the other assigned C exercises :
int any_odd_one(unsigned x) { return 2; /* what goes here? */ }
int threefourths(int x){ return 2; /* what goes here? */ }

signed integers

Continue discussion how signed and unsigned integers are stored and manipulated. See for example wikipedia: Two's_complement
What are the bytes in "(int) -1001"? (a) by hand (b) C code to display 'em.
Some obviously always true integer facts ... or are they?
#include <stdio.h> int main(){ int i; printf("Please enter i : "); scanf("%d", &i); printf(" i = %d \n", i); printf(" -i = %d \n", -i); printf(" -i < i ? %s \n", -i < i ? "yes" : "no"); printf(" -i + i = %d \n", -i + i); return 0; }
pg 52: practice problem 2.11
pg 57: typical ranges of C integral data
bits=? bytes=? min=? max=? char unsigned char short unsigned short int unsigned int long unsigned long long long unsigned long long
(the word "int" is optional after long)
/* WARNING: THIS IS BUGGY CODE (pg 77 of text) */ float sum_elements(float a[], unsigned length){ int i; float result = 0; for (i=0; i <= length-1; i++) result += a[i]; return result; }
... explain what the bug is.
Then check out practice 2.26, pg 77.

datalab

Explore the datalab code and btest infrastructure, and try some of 'em in class.

homework for next week

Which of these would be the most helpful?
1. continue on to finish chapter 2
2. more practice with integers and bit operations
3. more C practice with pointers: implement & test a binary tree in C
# API node *new_node(int data); // or (char* data_str) void insert(node *root, int data); int contains(node *root, int data); true if the binary tree has that data
after these calls node *root = new_node(10); insert(root, 20); insert(root, 4); insert(root, 6); printf("%s", contains(root, 11) ? 'yes' : 'no'); \\ prints 'no' printf("%s", contains(root, 6) ? 'yes' : 'no'); \\ prints 'yes'
and the tree would be in a configuration something like this.
10 / \ 4 20 / \ / \ 6
http://cs.marlboro.edu/ courses/ spring2013/systems/ notes/ Feb_7
last modified Thursday February 7 2013 12:16 pm EST