DOC PREVIEW
UW CSE 303 - Casts and List

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

'&$%CSE 303:Concepts and Tools for Software DevelopmentHal PerkinsAutumn 2007Lecture 11— C: casts, listsCSE303 Autumn 2007, Lecture 11 1'&$%Where are WeWe have learned most of the important stuff with C, so now we willmore toward idioms and larger programs.• Today: casts, linked lists• Next: The C pre-processor (stuff starting with #) and printf• Then: Post-overview, programming tools (make, gdb)Next Wednesday: Midterm in classLater: 2 lectures on C++ (48 less than necessary)I highly recommend understanding the code posted with this lecture;there is far too much to do that “on-the-fly” in the time we have.CSE303 Autumn 2007, Lecture 11 2'&$%The C typesThere are an infinite number of types in C, but only a few ways tomake them:• char, int, double, etc. (many more such as unsigned int)• void (a type no expression can have)• struct T where there is already a declaration for that struct type.• Array types (basically only for stack arrays and struct fields, everyuse is automatically converted to a pointer type)• t* where t is a type• union T, enum E (later, maybe)• function-pointer types (later)• typedefs (just expand to their definition)CSE303 Autumn 2007, Lecture 11 3'&$%Casts, part 1Syntax: (t)e where t is a type and e is an expression (same as Java).Semantics: It depends.• If e is a numeric type and t is a numeric type, this is a conversion.– To wider type, get same value– To narrower type, may not (will get mod)– From floating-point to integral, will round (may overflow)– From integral to floating-point, may round (but int to doublewon’t round on most machines)Note: Java is the same without the “most machines” part.Note: Lots of implicit conversions such as in function calls.Bottom Line: Conversions involve “real” operations; (double)3 is avery different bit pattern than (int)3.CSE303 Autumn 2007, Lecture 11 4'&$%Casts, part 2• If e has type t1*, then (t2*)e is a (pointer) cast.– You still have the same pointer (index into the address space).– Nothing “happens” at run-time.– You are just “getting around” the type system, making it easyto write any bits anywhere you want.– Old example: malloc has return type void*.void evil(int **p, int x) {int * q = (int*)p;*q = x;}void f(int **p) {evil(p,345);**p = 17; // writes 17 to address 345 (HYCSBWK)}Note: The C standard is more picky than we suggest, but few people know that and little code obeys the official rules.CSE303 Autumn 2007, Lecture 11 5'&$%Pointer casts continuedQuestions worth answering:• How does this compare to Java’s casts?– Unsafe, unchecked– Otherwise more similar than it seems• When should you use pointer casts in C?– For “generic” libraries (malloc, linked lists, swapping any twopointers, etc.)– For “subtyping” (later)• What about other casts?– Casts to/from struct types are compile-time errors.CSE303 Autumn 2007, Lecture 11 6'&$%Java castsJava casts (e.g., (Foo)e) explained to C programmers:• e evaluates to a pointer to an object.• Objects have “secret fields” at run-time indicating their class.• If the object’s secret field is Foo or a (transitive) subclass of Foo“succeed”. Else raise an exception. (Called a downcast)• If e’s (compile-time) type is a (transitive) subtype of Foo, thenthe compiler can “omit the check”. (Called an upcast)• If e’s (compile-time) type is neither a (transitive) subtype norsupertype of Foo, it is a compile-time error. (The cast could neversucceed.)CSE303 Autumn 2007, Lecture 11 7'&$%Linked listsLinked lists are a very common data structure.Building them in C:• Gives practice with pointers, structs, malloc, etc.• Leads to using casts for “generic” types.• Shows memory management problems if lists “share tails”.• Shows the trade-offs between lists and arrays.See the code! Understand the code!CSE303 Autumn 2007, Lecture 11


View Full Document

UW CSE 303 - Casts and List

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Casts and List
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Casts and List and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Casts and List 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?