1 Binghamton University Exam 1 Review CS220. 2 Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.

Slides:



Advertisements
Similar presentations
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
University of Washington Today: Floats! 1. University of Washington Today Topics: Floating Point Background: Fractional binary numbers IEEE floating point.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Arrays and Pointers in C Alan L. Cox
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
1 Review. 2 Creating a Runnable Program  What is the function of the compiler?  What is the function of the linker?  Java doesn't have a linker. If.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 University of Washington Fractional binary numbers What is ?
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSC 533: Programming Languages Spring 2016
Intro to Pointers in C CSSE 332 Operating Systems
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
CS 105 “Tour of the Black Holes of Computing!”
CSC 533: Programming Languages Spring 2015
2.4. Floating Point Numbers
A bit of C programming Lecture 3 Uli Raich.
Data Representation Binary Numbers Binary Addition
Chapter 6: Data Types Lectures # 10.
Topics IEEE Floating Point Standard Rounding Floating Point Operations
CS 105 “Tour of the Black Holes of Computing!”
C Programming Tutorial – Part I
Pointers and Memory Overview
Student Book An Introduction
Chapter 6 Floating Point
Programming Languages and Paradigms
Introduction to Linked Lists
Circular Buffers, Linked Lists
Unit 2 Programming.
Introduction to Abstract Data Types
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Pointers, Dynamic Data, and Reference Types
CS 105 “Tour of the Black Holes of Computing!”
Memory Allocation CS 217.
Pointers.
Variables Title slide variables.
CS 105 “Tour of the Black Holes of Computing!”
CS 105 “Tour of the Black Holes of Computing!”
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Recitation 5 – 2/19/01 Outline
Intro to Data Structures and ADTs
Computer Organization COMP 210
Homework Continue with K&R Chapter 5 Skipping sections for now
Computer Organization COMP 210
Java Programming Language
Data Structures & Algorithms
CS 105 “Tour of the Black Holes of Computing!”
Chapter 9: Pointers and String
CS213 Floating Point Topics IEEE Floating Point Standard Rounding
CS 105 “Tour of the Black Holes of Computing!”
EE 312 Exam I Review.
Pointers, Dynamic Data, and Reference Types
CSC 533: Programming Languages Spring 2019
SPL – PS2 C++ Memory Handling.
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
ECE 120 Midterm 1 HKN Review Session.
CS 105 “Tour of the Black Holes of Computing!”
Presentation transcript:

1 Binghamton University Exam 1 Review CS220

2 Binghamton University 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

3 Binghamton University 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

4 Binghamton University 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

5 Binghamton University 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

6 Binghamton University 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!

7 Binghamton University 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|-x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1) != 0 int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; Initialization

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: ExpressionDecimalBinary Zero ux y x>>1 TMax -TMin Tmin+TMin

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 2 E 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). DescriptionBinaryMEValue Minus zero Smallest denormalized Largest normalized One Positive infinity--+∞

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. (a) 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 (a) Interval [1,2) (b) Interval [2,3) (b) For denormalized numbers, what is the value E of the exponent after biasing? What is the largest value M of the significand? (c) 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(int argc, 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: (a) *ptr (b) *(ptr-3) (c) ptr-ptr2 (d) *(ptr-1)+2 (e) &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) A[1] (b) *(A[1]) (c) *(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: typedef struct 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; }