CS 330 Lecture 19 – Weak Typing in C
Agenda
- what ?s
- program this
- weak typing examples
- array allocation
- big endian
- is negative check for floats
- unions
- “polymorphism” in C
TODO
- The midterm exam will be during class next Wednesday. General topics we’ve covered include the shell, regular expressions, grammars, abstract syntax trees, the mechanics of assembly, and type systems through the lens of C. Exam questions are designed to use knowledge gained from homework and synthesize ideas. I’m not so interested in you having memorized a bunch of facts or API. See the past semesters’ exams under the Teaching tab.
Note
Let’s start with a Program This:
In pseudo-C code, write down an implementation of
itoa
, which converts anint
(assumed >= 0) into a C string.
From there we’ll look to some examples of weak typing in C, in which we undermine the type system to do some sometimes perfectly reasonable things. We’ll see why arrays traditionally couldn’t be allocated on the stack via consts, how we can check the negativity of a float, and how one might support a limited form of polymorphism for data structures.
Code
itoa.c
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError) Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec' from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem' from ./coderay:24:in `'