1 / 58

Hank Childs, University of Oregon

herman
Download Presentation

Hank Childs, University of Oregon

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 4: More C: Enums, Structs, & Unions Hank Childs, University of Oregon April 11th, 2014

  2. Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  3. Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  4. Announcement: HW • I am slowing down Proj 1C, 2A, 2B timelines • Proj 1C: now due Sun, not Sat • Proj 2A: now due Weds, not Monday • Proj 2B: now due Sat 4/19, not Fri 4/18. • (please start on it sooner)

  5. Announcements • Matt OH: Mon/Tues 4-5:30, DES 232 • Pavel OH: Weds/Thurs 4-5, DES 100 My Thurs OH: 10:30-12 I am very sorry to anyone inconvenienced. My Fri OH: whoops. New Fri OH: 12:30-1:30?

  6. Please come to OH!

  7. -I / #include • What is #include for? • What is –I? • Have you ever seen • #include <header.h> • #include “header.h” In –I path In current directory math.h and math330.h are different files

  8. Project 1B • Arguments: • $# • $1 • ./proj1 /tmp • (inside your script, $1=/tmp)

  9. Makefileexample: multiplier lib Causes other rules to get executed too First rule in file is executed You can tell Makefile to execute rule besides first one: “make doubler.o” How does this help with libraries, includes, installs?

  10. Great question from last time… How does this affect Makefiles?

  11. Unix command: tar • tar cvf 330.tar file1 file2 file3 • scp 330.tar @ix:~ • ssh ix • tar xvf 330.tar • ls file1 file2 file

  12. Example

  13. Standard Streams • Wikipedia: “preconnected input and output channels between a computer program and its environment (typically a text terminal) when it begins execution” • Three standard streams: • stdin(standard input) • stdout (standard output) • stderr (standard error) What mechanisms in C allow you to access standard streams?

  14. Redirecting stderr to stdout

  15. pipes in Unix shells • represented with “|” • output of one program becomes input to another program

  16. Questions? • Something unclear from last time? • Questions about HW? • Is everybody on Piazza? • vi tips?

  17. Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  18. Project 2A Binary file on web is now corrected. (Thank you Morgan!) 1 0

  19. Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  20. Enums • Enums make your own type • Type is “list of key words” • Enums are useful for code clarity • Always possible to do the same thing with integers • Be careful with enums • … you can “contaminate” a bunch of useful words

  21. enum example C keyword “enum” – means enum definition is coming This enum contains 6 different student types semi-colon!!!

  22. enum example

  23. enums translate to integers … and you can set their range

  24. But enums can be easier to maintain than integers If you had used integers, then this is a bigger change and likely to lead to bugs.

  25. Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  26. Data types • float • double • int • char • unsigned char All of these are simple data types

  27. Structs: a complex data type • Construct that defines a group of variables • Variables must be grouped together in contiguous memory • Also makes accessing variables easier … they are all part of the same grouping (the struct)

  28. struct syntax “.” accesses data members for a struct C keyword “struct” – means struct definition is coming This struct contains 6 doubles, meaning it is 48 bytes semi-colon!!! Declaring an instance

  29. Nested structs accesses dir part of Ray accesses directionZ part of Direction (part of Ray)

  30. typedef • typedef: tell compiler you want to define a new type saves you from having to type “struct” every time you declare a struct.

  31. Other uses for typedef • Declare a new type for code clarity • typedefintMilesPerHour; • Makes a new type called MilesPerHour. • MilesPerHour works exactly like an int. • Also used for enums & unions • same trick as for structs … typedef saves you a word

  32. Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  33. Unions • Union: special data type • store many different memory types in one memory location When dealing with this union, you can treat it as a float, as an int, or as 4 characters. This data structure has 4 bytes

  34. Unions Why are unions useful?

  35. Unions Example

  36. Unions Example

  37. Why are Unions useful? • Allows you to represent multiple data types simultaneously • But only if you know you want exactly one of them • Benefit is space efficiency, which leads to performance efficiency Unions are also useful for abstracting type. We will re-visit this when we talk about C++’s templates.

  38. Outline • Announcements/Review • Project 2A • Enums • Structs • Unions • Project 2B • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  39. Project 2B • Prompt posted tonight, Saturday (8 days) • Define structs, union of structs, functions that manipulate structs

  40. Outline • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  41. Problem with C…

  42. Problem with C… No checking of type…

  43. Problem is fixed with C++…

  44. Problem is fixed with C++…

  45. Mangling • Mangling refers to combing information about arguments and “mangling” it with function name. • Way of ensuring that you don’t mix up functions. • Why not return type too? • Causes problems with compiler mismatches • C++ compilers haven’t standardized. • Can’t take library from icpc and combine it with g++.

  46. C++ will let you overload functions with different types

  47. C++ also gives you access to mangling via “namespaces” Functions or variables within a namespace are accessed with “::”

  48. C++ also gives you access to mangling via “namespaces” The “using” keyword makes all functions and variables from a namespace available without needing “::”. And you can still access other namespaces.

  49. Outline • Symbols and mangling (if time) • Killing and suspending jobs (if time) • Web pages (if time)

  50. Killing programs, part 1 • If you want to kill a program that your shell is currently running, then it is easy: • press Control-C • What happens? • OS will send a signal to the program. • Programs can catch the signal and ignore, but most programs just die.

More Related