170 likes | 281 Views
Exam 1 Review CS220. Birds eye view -- Topics. Information Representation Bit-level manipulations Integer representation, conversion, casting, arithmetics … Floating point representation, covnersion , casting, arithmetics , rounding, ... The C programming Language Pointers
E N D
Birds eye view -- Topics • Information Representation • Bit-level manipulations • Integer representation, conversion, casting, arithmetics… • Floating point representation, covnersion, casting, arithmetics, rounding, ... • The C programming Language • Pointers • Arrays and pointer arithmetics • Dynamic memory allocation • Structs • Data structures such as linked lists • Chapter 2 in the book, and the C tutorial on the website
Concerned about C; major concepts • Basic Syntax: • Similar to Java (no objects, and Java has no pointers) • C++ is a superset of C (C has no classes, templates, etc…) • I assumed you know basic syntax • if this is a wrong assumption, please let me know • Module 1 in tutorial (self-study) has some help • Pointers are variables that hold an address • They have a type, representing the type stored at the memory address • & and * • Pointer arithmetic in units of the type • Can be used to get around C’s pass by value
C major concepts (cont’d) • Arrays are allocated as a contiguous block of memory • Elements are stored in order • Row major for multi-dimensional arrays • Use indices to compute memory location of element • Can use pointers to access the array elements • Strings are arrays of characters terminated by a \0 (NULL character) • Multi-dimensional arrays as arrays of pointers are different; see the examples in the slides
C Major concepts, continued • Two types of variables: those allocated by compiler, and those allocated by programmer dynamically • Static memory: when you declare a variable, or an array compiler allocates space and manages (usually on the stack) • You don’t need to free it • Have to provide size at compile time; cannot change it later • Dynamic memory: Alternatively, when you use malloc, you dynamically ask for a chunk of memory (on the heap) • You manage it manually
C Major concepts; continued • Structs are a composition of variables (kind of a degenerate object) • Assignment of a struct to another causes a shallow copy • Can nest structs as long as there is no loop • Can include pointers to structs as members • including to structs of the same type • Can treat them as any other variable: e.g., allocate arrays of structs, malloc them, etc… • Should get familiar with use to build data structures such as linked lists Lets do some problems!
Integer C Puzzles • x < 0 ((x*2) < 0) • ux >= 0 • x & 7 == 7 (x<<30) < 0 • ux > -1 • x > y -x < -y • x * x >= 0 • x > 0 && y > 0 x + y > 0 • x >= 0 -x <= 0 • x <= 0 -x >= 0 • (x|-x)>>31 == -1 • ux>> 3 == ux/8 • x >> 3 == x/8 • x & (x-1) != 0 Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y;
Problems • Consider a 6-bit machine, which uses 2s complement for negative numbers. Assume shorts are represented using 3 bits. short sy = -3; int y = sy; Int x=-17; unsigned ux=x; Fill in theTable:
Consider an 8-bit floating point number based on IEEE floating point format. There is a sign bit, 3 exponent bits, with bias of 3, and 4 bits of fraction. The rules are like those in the IEEE standard (normalized, denormalized, representation of 0, infinity and NaN). The number has the form V=(-1)s x M x 2E Fill in the table below (Binary is the 8-bit binary representation, M is the significand, E is the exponent, and V is the numeric value).
Consider a 12 bit floating point representation based on the IEEE floating point format. There is one sign bit, 4 exponent bits, and 7 significand bits. • How many floating point numbers are in the following intervals [a,b)? For each interval, count the numbers of x such that a ≤ x < b • Interval [1,2) • Interval [2,3) • For denormalized numbers, what is the value E of the exponent after biasing? What is the largest value M of the significand? • For normalized numbers, what is the smallest value E of the exponent after biasing? What is the largest value E of the exponent after biasing? What is the largest value M of the significand?
Some C problems • General • Make sure you understand quiz; will make key available • K&R is an excellent source of practice problems • Tutorial links for exercises. • Some more in following slides. Will make more available this evening and tomorrow.
Look at the following code segments. If they produce an output, provide it. If they have an error, clearly identify it and discuss its effect. int main(intargc, char* argv[] ){ int x = 10; int* ptr = &x; ptr++; printf(“%x %d \n”, ptr, *ptr); }
Study the following code segment. What is the purpose of the foo function? Does it work as intended? If not, how would you fix it? Show any memory leaks. int n=10; int main() { int ** A=malloc(sizeof(int *)*n); //could I use int * A[n]; instead? … foo(&A); n*=2; … } int foo(int ***array){ int **arrayint = (int**)malloc(2*n*sizeof(int *)); for(i=0; i<n;i++) arrayint[i]=(*array)[i]; array=&arrayint; }
Consider the following code: int A[]={1,2,3,4,5}; int * ptr = A + 5; int *ptr2 = A; What are the values of the following? If undefined say why: • *ptr • *(ptr-3) • ptr-ptr2 • *(ptr-1)+2 • &ptr
Consider the following code int A[2][3] = {{1,2,3},{4,5,6}} and assume that A[0]=0xFFAA0000 What is the value of the following? • A[1] • *(A[1]) • *(A[0]+1)
A linked list that ends with a self-loop (the last element pointing to itself) is called a sloop. Alternatively, linked lists could end with a null pointer. Given a linked list made up of the following struct elements: typedefstruct node { int data; struct node *next; } node; Write a function that returns TRUE if the linked list is a sloop, otherwise false. Assume that there are no other loops in the linked list.
The following function (addend) is supposed to add a node to the end of a sloop. It must retain the sloop property. However, the code contains some bugs. Find the bugs and fix them by rewriting or adding new statements. node * addend(node *head,int data) { node * q = malloc (sizeof(node)); while(head != head->next) head=head->next; head->data=data; head->next=q; }