E N D
CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 15: Potpourri: const, globals& static, scope Hank Childs, University of Oregon May 16th, 2014
Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E, 4B • global, static • scope
Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E, 4B • global, static • scope
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
Announcements • Projects: • 3E assigned today, due May 22nd • 4B assigned May 17th, due May 24th • Debug other students bad programs
Announcements • netpbm • macports • brew
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)
Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E, 4B • global, static • scope
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
C++ streams example (cascading & endl)
Three pre-defined streams • cout<= => fprintf(stdout, … • cerr <= => fprintf(stderr, … • cin <= => fscanf(stdin, …
cerr • Works like cout, but prints to stderr • Always flushes everything immediately! “See the error”
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
Memory Errors • Array bounds read • Array bounds write
Memory Errors • Free memory read / free memory write Vocabulary: “dangling pointer”: pointer that points to memory that has already been freed.
Memory Errors • Freeing unallocated memory
Memory Errors • Freeing non-heap memory
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?
Memory Errors • Unitialized memory read
Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E • global, static • scope
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.
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
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
Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E • global, static • scope
const • const: • is a keyword in C and C++ • qualifies variables • is a mechanism for preventing write access to variables
const example const keyword modifies int The compiler enforces const… just like public/private access controls
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.
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
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.
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
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
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
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
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
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!!
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.
const functions with objects If a class method is declared as const, then you can call those methods with pointers. constafter method name
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.
Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E • global, static • scope
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
Project 3E • Worth 5% of your grade • Assigned today, due May 22nd • Also component for “const”
Outline • Announcements • Review • Project 3D, 4A, 4B • const • Project 3E • global, static • scope
globals • You can create global variables that exist outside functions.
global variables • global variables are initialized before you enter main
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
Externs: mechanism for unifying global variables across multiple files extern: there’s a global variable, and it lives in a different file.
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