CS 208 w20 lecture 5 outline

1 malloc and string.h

1.1 Every call to malloc should have a corresponding call to free

  • Each malloc call, assuming it succeeds, acquires a new chunk of memory from the operating system, returns a pointer to this chunk
  • free(p) lets the operating system know that the chunk pointed to by p is now available for other programs to use
  • For lab 0: every call you make to malloc should have a matching call to free in q_free
    • Dereferencing freed memory has undefined behavior, so you need to save anything you might need before freeing

1.2 The C string library

  • #include <string.h> gives you access to some useful functions for working with C strings
  • http://cplusplus.com is an excellent source of documentation
  • strlen(s) returns the length of the string (char *) s, not counting the null terminator
  • strcpy(dest, src) copies the string at dest to src (both arguments are of type char * (pointers to the start of a char array)
    • strncpy lets you provide a maximum number of characters to copy

2 Fractional Binary Numbers

fractional-binary.png

  • Example 0b10.1010 = \(1\times2^1 + 1\times2^{-1} + 1\times2^{-3} = 2.625\)
  • Exercise: what's the closest we can get to 1/3 with 6 bits? .010101 (1/4 + 1/16 + 1/64 = 0.328125)
fraction decimal
1/2 0.5
1/4 0.25
1/8 0.125
1/16 0.0625
1/32 0.03125
1/64 0.015625

2.1 Scientific Notation

2.1.1 Base 10

scientific-notation-10.png

2.1.2 Base 2

scientific-notation-2.png

Computer arithmetic that supports this called floating point due to the "floating" of the binary point

2.1.3 Normalization

  • In this notation, normalized form means having exactly one non-zero digit to the left of the decimal/binary point
  • There are multiple ways to represent one billionth (1/1,000,000,000) in base 10:
    • Normalized: \(1.0\times10^{-9}\)
    • Denormalized: \(0.1\times10^{-8}\), \(10.0\times10^{-10}\)

3 IEEE Floating Point

  • We will not cover all the complexities of a 50-page standard
  • Saved us from the wild west of every manufacturer designing their own
  • What do we want from a floating point standard?
    • Scientists/numerical analysts want them to be as real as possible
    • Engineers want them to be easy to implement and fast
    • Scientists mostly won, floating-point operations can be several times slower
  • Basic idea: represent numbers in binary scientific notation

3.1 Representation

  • \(V = (-1)^s \times M \times 2^E\)
    • sign \(s\) indicates positive or negative (sign bit for 0 is special case)
    • significand \(M\) fractional binary number between 1 and \(2 - \epsilon\) or between 0 and \(1 - \epsilon\)
    • exponent \(E\) weights by a power of 2 (can be negative power)
  • Exercise: with 1 sign bit, 3 exponent bits, and 2 significand bits, how close can we get to 1/3
    • treat exponent as 3-bit two's complement integer, treat significand bits as fractional part \(f\) of the binary number \(1.f\)
    • 0 110 01 (sign exponent significand) yields \(1.25 \times 2^{-2} = 0.3125\)
    • not as close as with a 6-bit binary fraction we could structure however we wanted, but an unstructured representation would require extra bits to locate the binary point