Computer
Systems

Fall 2010
course
navigation

Sep 9

Discuss more C syntax, and start the chapter 2 material. Please do ask questions!
Start with an example : pointers, command line, a bit of bit operations :
Where to put the * : // What's the difference between these two? int* x; int *y;
Pointers and arrays : int x[100]; // What is "x" all by itself? int* x0_ptr = &x[0]; // use some printf statements to look at these. int* x1_ptr = &x[1]; *(x0_ptr +3) = 7; // What the heck is this?
Heap vs stack : write a program with two simple functions. Print the address of some local variables in the functions, while they're running, with one called from the other, back and forth. Also allocate some heap memory chunks, and look at their addresses. Discuss. (If we don't get through this, it may turn into homework...)
Sizes of things and integer overflow : #include <stdio.h> #include <limits.h> #include <float.h> int main(){ printf("\n"); printf(" sizeof(int) = %lu bytes \n", sizeof(int)); printf(" sizeof(long) = %lu bytes \n", sizeof(long)); printf(" sizeof(float) = %lu bytes \n", sizeof(float)); printf(" sizeof(double) = %lu bytes \n", sizeof(double)); printf(" sizeof(char*) = %lu bytes \n", sizeof(char*)); printf("\n"); printf(" INT_MAX, LONG_MAX = %i, %li \n", INT_MAX, LONG_MAX); printf(" FLT_DIG, FLT_EPSILON, FLT_MAX = %i, %g, %g \n", FLT_DIG, FLT_EPSILON, FLT_MAX); printf(" DBL_DIG, DBL_EPSILON, DBL_MAX = %i, %g, %g \n", DBL_DIG, DBL_EPSILON, DBL_MAX); printf("\n"); // overflow; see CS:APP2e pg 30 // This gives a compilation time warning: // line 12 : int i = 200 * 300 * 400 * 500 // $ make sizes // sizes.c: In function 'main': // sizes.c:12: warning: integer overflow in expression // But with this code, no warning at compile time: can't tell. int i = 1; i *= 200 * 300; i *= 400 * 500; printf(" int i = 200 * 300 * 400 * 500 = %i \n", i); // gives " int i = 200 * 300 * 400 * 500 = -884901888 " // Since 2**32 is 0 in this "int" number system, // the way to get that negative number is to find // (200*300*400*500 - n*2**32) for the smallest // value of n that makes it less than 2**31; // that turns out to be n=3 in this case. // In other words : // int32(x) = (x % 2**32 >= 2**31) ? (x % 2**32) - 2**32 : x % 2**32 // or even better, in (say) ruby // def int32(x); (x + 2**31) % 2**32 - 2**31; end printf("\n"); return 0; }
Sizes of things; big/little endian : show bytes
http://cs.marlboro.edu/ courses/ fall2010/systems/ notes/ Sep_9
last modified Wednesday January 30 2013 11:13 pm EST