E N D
CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 8: Building Large Projects, Mangling and Namespaces Beginning on Project 3 Hank Childs, University of Oregon April 25th, 2014
Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages
Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages
Schedule changes for this week • Lecture #8: right now • Lecture #9: Friday 10-11, 11-12, and 3:30-4:30 • We will do Proj2N for lecture #9 • No lecture on May 9th • (we are banking an extra this week) • Proj 2D: graded in class today • Proj 2C: due Monday
Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages
Memory Addresses • Every location in memory has an address associated with it • Locations in memory are represented in hexadecimal Code Data Stack 0x7fff55bc 0x7fff55b8 Free Heap Memory addresses descend in the stack, ascend in the heap.
Pointers • Pointers store locations in memory • “&”: unary operator that gives the address of a variable. int x; int *yp = &x;
‘*’ operator • Let “ptr” be a pointer • Then “*ptr” returns value in the address that ptr points to. • * = “dereference operator”
Pointer Arithmetic • You can combine pointers and integers to get new pointer locations ptr + 1 ptr + sizeof(type) bytes
Arrays • Arrays: container that has multiple elements of identical type, all stored in contiguous memory int A[10]; 10 integers, stored in 40 consecutive bytes (assuming sizeof(int) == 4) Arrays are just pointers. You can use arrays and pointers interchangeably.
[ ] operator • [ ] is a way of dereferencing memory • Recall that ‘*’ is the dereference operator • A[0] <= => *A • A[5] <= => *(A+5);
Pointers to pointers • Remember: pointer points to a location in memory • We’ve been considering cases where locations in memory are arrays of integers • But locations in memory could be pointer themselves Code Data Stack 0x7fff55bc 0x7fff55b8 Free Heap
Call by reference / call by value • Refers to how parameters are passed to a function. • Call by reference: send a reference (pointer) as a function parameter • Side effects in that function affect the variable in the calling function • Call by value: send the value of the variable as a function parameter • Side effects in that function don’t affect the variable in the calling function
Questions? • Something unclear from last time? • Questions about HW? • vi tips?
Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages
Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages
3 files: prototypes.h, rectangle.c, driver.c prototypes.h structRectangle; void IntializeRectangle(struct Rectangle *r, double v1, double v2, double v3, double v4); rectangle.c struct Rectangle { double minX, maxX, minY, maxY; }; void IntializeRectangle(struct Rectangle *r, double v1, double v2, double v3, double v4) { r->minX = v1;r->maxX = v2; r->minY = v3; r->maxY = v4; } driver.c #include <prototypes.h> int main() { struct Rectangle r; InitializeRectangle(r, 0, 1, 0, 1.5); }
Review on compilation • gcc –c: build an object file (.o), i.e., binary code that can directly run on the architecture • Then the binary can be generated from the object files. • Libraries are a mechanism for grouping up a bunch of related object files • They are assembled together using a program called an archiver (ar) • You can also just use object files directly when linking.
Makefiles • Consists of rules • Rule syntax: target: dependency1 dep2 … depN <tab>command1 <tab>command2 Quiz: write down a Makefile for a program called proj2B. Again, the file names are prototypes.h, driver.c, rectangle.c
Makefile for prototypes.h, rectangle.c, driver.c Makefile proj2B: driver.crectangle.cprototypes.h gcc -I. -c rectangle.c gcc -I. -c driver.c gcc -o proj2B driver.orectangle.o Is this a good Makefile? What’s the problem with it?
Makefile for prototypes.h, rectangle.c, driver.c Makefile proj2B: rectangle.odriver.o gcc -o proj2B driver.orectangle.o driver.o: prototypes.hdriver.c gcc -I. -c driver.c rectangle.o: prototypes.hrectangle.c gcc -I. -c rectangle.c
Definition of Rectangle in rectangle.cWhy is this a problem? prototypes.h structRectangle; void IntializeRectangle(struct Rectangle *r, double v1, double v2, double v3, double v4); rectangle.c struct Rectangle { double minX, maxX, minY, maxY; }; void IntializeRectangle(struct Rectangle *r, double v1, double v2, double v3, double v4) { r->minX = v1;r->maxX = v2; r->minY = v3; r->maxY = v4; } “gcc –c driver.c” needs to make an object file. It needs info about Rectangle then, not later. driver.c #include <prototypes.h> int main() { struct Rectangle r; InitializeRectangle(r, 0, 1, 0, 1.5); }
New gcc option: “gcc –E” The fix is to make sure driver.c has access to the Rectangle struct definition. driver.c #include <prototypes.h> int main() { struct Rectangle r; InitializeRectangle(r, 0, 1, 0, 1.5); } gcc –E shows what the compiler sees after satisfying “preprocessing”, which includes steps like “#include”. # 1 "driver.c" # 1 "<built-in>" 1 # 1 "<built-in>" 3 # 162 "<built-in>" 3 # 1 "<command line>" 1 # 1 "<built-in>" 2 # 1 "driver.c" 2 # 1 "./prototypes.h" 1 structRectangle; voidInitializeRectangle(structRectangle *r, double v1, double v2, double v3, double v4); # 2 "driver.c" 2 intmain() { structRectangle r; InitializeRectangle(r, 0, 1, 0, 1.5); } gcc–E –I. driver.c This is it. If the compiler can’t figure out how to make object file with this, then it has to give up.
Preprocessor • Preprocessor: • takes an input program • produces another program (which is then compiled) • C has a separate language for preprocessing • Different syntax than C • Uses macros (“#”) macro (“macroinstruction”): rule for replacing input characters with output characters
Preprocessor Phases • Resolve #includes • (we understand #include phase) • Conditional compilation • Macro replacement • Special macros
#define compilation This is an example of macro replacement.
Conditional compilation controlled via compiler flags This is how configure/cmake controls the compilation.
4 files: struct.h, prototypes.h, rectangle.c, driver.c struct.h struct Rectangle { double minX, maxX, minY, maxY; }; prototypes.h #include <struct.h> void InitializeRectangle(struct Rectangle *r, double v1, double v2, double v3, double v4); rectangle.c #include <struct.h> #include <prototypes.h> void IntializeRectangle(struct Rectangle *r, double v1, double v2, double v3, double v4) { r->minX = v1;r->maxX = v2; r->minY = v3; r->maxY = v4; } #include <struct.h> #include <prototypes.h> int main() { struct Rectangle r; InitializeRectangle(&r, 0, 1, 0, 1.5); } driver.c What is the problem with this configuration?
#ifndef/ #define to the rescue struct.h #ifndef RECTANGLE_330 #define RECTANGLE_330 struct Rectangle { double minX, maxX, minY, maxY; }; #endif Why does this work? This problem comes up a lot with big projects, and especially with C++.
There is more to macros… • Macros are powerful & can be used to generate custom code. • Beyond what we will do here. • Two special macros that are useful: • __FILE__ and __LINE__
Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages
Problem with C… No checking of type…
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++.
C++ also gives you access to mangling via “namespaces” Functions or variables within a namespace are accessed with “::”
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.
Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages
Project #3A: background • Definitions: • Image: 2D array of pixels • Pixel: A minute area of illumination on a display screen, one of many from which an image is composed. • Pixels are made up of three colors: Red, Green, Blue (RGB) • Amount of each color scored from 0 to 1 • 100% Red + 100% Green + 0% Blue = Yellow • 100% Red + 0% Green + 100 %Blue = Purple • 0% Red + 100% Green + 0% Blue = Cyan • 100% Red + 100% Blue + 100% Green = White
Project #3A: background • Colors are 0->1, but how much resolution is needed? How many bits should you use to represent the color? • Can your eye tell the difference between 8 bits and 32 bits? • No. Human eye can distinguish ~10M colors. • 8bits * 3 colors = 24 bits = ~16M colors. • Red = (255,0,0) • Green = (0,255,0) • Blue = (0,0,255)
Project 3A • Prompt posted by Saturday night • Due on Friday, May 2nd • Tasks: • Struct to contain image • Read image from file (simplified format) • Write image to file (simplified format) • Function to modify image • Program that puts it all together We will be modifying this code throughout the quarter… (expect you will have to retrofit what you are writing now)
Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages
Memory Errors • Array bounds read • Array bounds write