1 / 67

Hank Childs, University of Oregon

mason
Télécharger la présentation

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 15: Potpourri: const, globals& static, scope Hank Childs, University of Oregon May 16th, 2014

  2. Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E, 4B • global, static • scope

  3. Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E, 4B • global, static • scope

  4. Announcements • Projects: • 3D assigned Wednesday, due Tuesday • “Optional” • You get 0%/3% if you skip (so not optional) • But you don’t need it for 3E, 3F, etc • 4A assigned today, due Saturday at NOON • Hard part will be learning Subversion • Should take about an hour

  5. Announcements • Projects: • 3E assigned today, due May 22nd • 4B assigned May 17th, due May 24th • Debug other students bad programs

  6. Announcements • netpbm • macports • brew

  7. Announcements: CIS441 • Teaching CIS441 in Fall • First 5 weeks: building graphics system in SW • Next 2 weeks: learn OpenGL • Last 3 week: final project (judged by local gaming professionals) (show some examples)

  8. Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E, 4B • global, static • scope

  9. New operators: << and >> • “<<”: Insertion operator • “>>”: Extraction operator • Operator overloading: you can define what it means to insert or extract your object. • Often used in conjunction with “streams” • Recall our earlier experience with C streams • stderr, stdout, stdin • Streams are communication channels

  10. C++ streams example (cascading & endl)

  11. Three pre-defined streams • cout<= => fprintf(stdout, … • cerr <= => fprintf(stderr, … • cin <= => fscanf(stdin, …

  12. cin in action

  13. cerr • Works like cout, but prints to stderr • Always flushes everything immediately! “See the error”

  14. fstream • ifstream: input stream that does file I/O • ofstream: output stream that does file I/O • Not lecturing on this, since it follows from: • C file I/O • C++ streams http://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm

  15. Memory Errors • Array bounds read • Array bounds write

  16. Memory Errors • Free memory read / free memory write Vocabulary: “dangling pointer”: pointer that points to memory that has already been freed.

  17. Memory Errors • Freeing unallocated memory

  18. Memory Errors • Freeing non-heap memory

  19. Memory Errors • NULL pointer read / write • NULL is never a valid location to read from or write to, and accessing them results in a “segmentation fault” • …. remember those memory segments?

  20. Memory Errors • Unitialized memory read

  21. Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E • global, static • scope

  22. Project 3D • Assigned: Weds, 5/14 • Due: Tuesday, 5/20 • Important: if you skip this project, you will still be able to do future projects (3E, 3F, etc) • Assignment: • Write PNMreaderCPP and PNMwriterCPP … new version of the file reader and writer that use fstream.

  23. Project 4A • Goal: • Write a program that contains 20-40 lines of code that has one or more memory errors • The program should compile • It may crash, it may not (your choice) • Details: • Worth 1% of your grade • Assigned Weds 5/14, due Saturday 5/17 at NOON • Submission: check your program into a “Subversion” repository Project 4B will involve all us debugging each other’s projects

  24. Project 4B • Goal: • Debug 6 other student programs • I choose the 6, not you • Details: • Worth 3% of your grade • Assigned Saturday 5/17, due Saturday 5/24 • Submission: check your answers into a “Subversion” repository

  25. Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E • global, static • scope

  26. const • const: • is a keyword in C and C++ • qualifies variables • is a mechanism for preventing write access to variables

  27. const example const keyword modifies int The compiler enforces const… just like public/private access controls

  28. Efficiency Are any of the three for loops faster than the others? Why or why not? Answer: X is probably faster than Y … compiler can do optimizations where it doesn’t have to do “i < X“ comparisons (loop unrolling) Answer: NumIterations is slowest … overhead for function calls.

  29. const arguments to functions • Functions can use const to guarantee to the calling function that they won’t modify the arguments passed in. guarantees function won’t modify the Image read function can’t make the same guarantee

  30. const pointers • Assume a pointer named “P” • Two distinct ideas: • P points to something that is constant • P may change, but you cannot modify what it points to via P • P must always point to the same thing, but the thing P points to may change.

  31. const pointers int X = 4; int *P = &X; Idea #2: violates const: “int Y = 5; P = &Y;” OK: “*P = 3;” Idea #1: violates const: “*P = 3;” OK: “int Y = 5; P = &Y;” pointer can’t change, but you canmodify the thing it points to pointer can change, but you can’t modify the thing it points to

  32. const pointers int X = 4; int *P = &X; Idea #3: violates const: “*P = 3;” “int Y = 5; P = &Y;” OK: none pointer can’t change, and you can’t modify the thing it points to

  33. const pointers int X = 4; int *P = &X; Idea #1: violates const: “*P = 3;” OK: “int Y = 5; P = &Y;” const goes before type pointer can change, but you can’t modify the thing it points to

  34. const pointers int X = 4; int *P = &X; Idea #2: violates const: “int Y = 5; P = &Y;” OK: “*P = 3;” const goes after * pointer can’t change, but you canmodify the thing it points to

  35. const pointers int X = 4; int *P = &X; Idea #3: violates const: “*P = 3;” “int Y = 5; P = &Y;” OK: none constin both places pointer can’t change, and you can’t modify the thing it points to

  36. const usage • class Image; • const Image *ptr; • Used a lot: offering the guarantee that the function won’t change the Image ptr points to • Image * constptr; • Helps with efficiency. Rarely need to worry about this. • const Image * constptr; • Interview question!!

  37. Very common issue with const and objects How does compiler know GetNumberOfPixels doesn’t modify an Image? We know, because we can see the implementation. But, in large projects, compiler can’t see implementation for everything.

  38. const functions with objects If a class method is declared as const, then you can call those methods with pointers. constafter method name

  39. mutable • mutable: special keyword for modifying data members of a class • If a data member is mutable, then it can be modified in a const method of the class. • Comes up rarely in practice.

  40. Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E • global, static • scope

  41. Project 3E

  42. Project 3E • You will need to think about how to accomplish the data flow execution pattern and think about how to extend your implementation to make it work. • This prompt is vaguer than some previous ones • … not all of the details are there on how to do it

  43. Project 3E • Worth 5% of your grade • Assigned today, due May 22nd • Also component for “const”

  44. Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E • global, static • scope

  45. globals • You can create global variables that exist outside functions.

  46. global variables • global variables are initialized before you enter main

  47. Storage of global variables… • global variables are stored in a special part of memory • “data segment” (not heap, not stack) • If you re-use global names, you can have collisions

  48. Externs: mechanism for unifying global variables across multiple files extern: there’s a global variable, and it lives in a different file.

  49. static • static memory: third kind of memory allocation • reserved at compile time • contrasts with dynamic (heap) and automatic (stack) memory allocations • accomplished via keyword that modifies variables There are three distinct usages of statics

  50. static usage #1: persistency within a function

More Related