CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 8:Building Large Projects,Mangling and NamespacesBeginning on Project 3Hank Childs, University of OregonApril 25th, 2014Outline
Announcements/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpagesOutline
Announcements/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpagesSchedule changes for this week
Lecture #8: right nowLecture #9: Friday 10-11, 11-12, and 3:30-4:30We will do Proj2N for lecture #9 No lecture on May 9th(we are banking an extra this week)Proj 2D: graded in class todayProj 2C: due MondayOutline
Announcements/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpagesMemory Addresses
Every location in memory has an address associated with itLocations in memory are represented in hexadecimalCodeDataStack0x7fff55bc0x7fff55b8FreeHeapMemory 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 pointerThen “*ptr” returns value in the address that ptr points to. * = “dereference operator”Pointer Arithmetic
You can combine pointers and integers to get new pointer locationsptr + 1ptr + sizeof(type) bytesArrays
Arrays: container that has multiple elements of identical type, all stored in contiguous memoryint 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 memoryRecall that ‘*’ is the dereference operatorA[0] <= => *AA[5] <= => *(A+5);Pointers to pointers
Remember: pointer points to a location in memoryWe’ve been considering cases where locations in memory are arrays of integersBut locations in memory could be pointer themselvesCodeDataStack0x7fff55bc0x7fff55b8FreeHeapCall by reference / call by value
Refers to how parameters are passed to a function.Call by reference: send a reference (pointer) as a function parameterSide effects in that function affect the variable in the calling functionCall by value: send the value of the variable as a function parameterSide effects in that function don’t affect the variable in the calling functionQuestions?
Something unclear from last time?Questions about HW?vi tips?Outline
Announcements/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpagesOutline
Announcements/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpages3 files: prototypes.h, rectangle.c, driver.cprototypes.hstructRectangle;void IntializeRectangle(struct Rectangle *r, double v1, double v2, double v3, double v4);rectangle.cstruct 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 architectureThen the binary can be generated from the object files.Libraries are a mechanism for grouping up a bunch of related object filesThey are assembled together using a program called an archiver (ar)You can also just use object files directly when linking.Makefiles
Consists of rulesRule syntax:target: dependency1 dep2 … depN<tab>command1<tab>command2Quiz: write down a Makefile for a program called proj2B. Again, the file names are prototypes.h, driver.c, rectangle.cMakefile for prototypes.h, rectangle.c, driver.cMakefileproj2B: driver.crectangle.cprototypes.hgcc -I. -c rectangle.cgcc -I. -c driver.cgcc -o proj2B driver.orectangle.oIs this a good Makefile?What’s the problem with it?Makefile for prototypes.h, rectangle.c, driver.cMakefileproj2B: rectangle.odriver.ogcc -o proj2B driver.orectangle.odriver.o: prototypes.hdriver.cgcc -I. -c driver.crectangle.o: prototypes.hrectangle.cgcc -I. -c rectangle.cDefinition of Rectangle in rectangle.cWhy is this a problem?prototypes.hstructRectangle;void IntializeRectangle(struct Rectangle *r, double v1, double v2, double v3, double v4);rectangle.cstruct 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" 1structRectangle;voidInitializeRectangle(structRectangle *r, double v1, double v2, double v3, double v4);# 2 "driver.c" 2intmain(){structRectangle r;InitializeRectangle(r, 0, 1, 0, 1.5);}gcc–E –I. driver.cThis 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 programproduces another program (which is then compiled)C has a separate language for preprocessingDifferent syntax than CUses macros (“#”)macro (“macroinstruction”): rule for replacing input characters with output charactersPreprocessor Phases
Resolve #includes(we understand #include phase)Conditional compilationMacro replacementSpecial macros#define compilationThis is an example of macro replacement.#define via gcc command-line optionConflicting –D and #defineConditional compilationConditional compilation controlled via compiler flagsThis is how configure/cmake controls the compilation.4 files: struct.h, prototypes.h, rectangle.c, driver.cstruct.hstruct 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.cWhat is the problem with this configuration?Compilation errorgcc –E rectangle.c#ifndef/ #define to the rescuestruct.h#ifndef RECTANGLE_330#define RECTANGLE_330struct Rectangle{ double minX, maxX, minY, maxY;};#endifWhy 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/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpagesProblem with C…Problem with C…No checking of type…Problem is fixed with C++…Problem is fixed with C++…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 mismatchesC++ compilers haven’t standardized.Can’t take library from icpc and combine it with g++.C++ will let you overload functions with different typesC++ 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/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpagesProject #3A: background
Definitions:Image: 2D array of pixelsPixel: 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 1100% Red + 100% Green + 0% Blue = Yellow100% Red + 0% Green + 100 %Blue = Purple0% Red + 100% Green + 0% Blue = Cyan100% Red + 100% Blue + 100% Green = WhiteProject #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 nightDue on Friday, May 2ndTasks:Struct to contain imageRead image from file (simplified format)Write image to file (simplified format)Function to modify imageProgram that puts it all togetherWe will be modifying this code throughout the quarter… (expect you will have to retrofit what you are writing now)Outline
Announcements/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpagesMemory Errors
Array bounds readArray bounds writeMemory Errors
Free memory read / free memory writeWhen does this happen in real-world scenarios?Memory Errors
Freeing unallocated memoryWhen does this happen in real-world scenarios?Vocabulary: “dangling pointer”: pointer that points to memory that has already been freed.Memory Errors
Freeing non-heap memoryWhen does this happen in real-world scenarios?Memory Errors
NULL pointer read / writeNULL is never a valid location to read from or write to, and accessing them results in a “segmentation fault”…. remember those memory segments?When does this happen in real-world scenarios?Memory Errors
Unitialized memory readWhen does this happen in real-world scenarios?Outline
Announcements/ReviewGrade project 2DBuilding Large ProjectsMangling with C/C++Project 3ABackground on imagesBonus topicsMemory ErrorsWebpagesWeb pages
ssh –l <user name> ix.cs.uoregon.educd public_htmlput something in index.html it will show up as http://ix.cs.uoregon.edu/~<username>Web pages
You can also exchange files this wayscpfile.pdf <username>@ix.cs.uoregon.edu:~/public_htmlpoint people to http://ix.cs.uoregon.edu/~<username>/file.pdfNote that ~/public_html/dir1shows up as http://ix.cs.uoregon.edu/~<username>/dir1(“~/dir1” is not accessible via web)