Hank Childs, University of Oregon

Please download to get full document.

View again

of 58
5 views
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.

Download

Document Related
Document Description
CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / /…
Document Share
Document Transcript
CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 8:Building Large Projects,Mangling and NamespacesBeginning on Project 3Hank Childs, University of OregonApril 25th, 2014Outline
  • 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
  • CodeDataStack0x7fff55bc0x7fff55b8FreeHeapMemory 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) bytesArrays
  • 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
  • CodeDataStack0x7fff55bc0x7fff55b8FreeHeapCall 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.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 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>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 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 charactersPreprocessor Phases
  • Resolve #includes
  • (we understand #include phase)
  • Conditional compilation
  • Macro replacement
  • Special 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/Review
  • Grade project 2D
  • Building Large Projects
  • Mangling with C/C++
  • Project 3A
  • Background on images
  • Bonus topics
  • Memory Errors
  • Webpages
  • Problem 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 mismatches
  • C++ 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/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
  • Memory Errors
  • Free memory read / free memory write
  • When does this happen in real-world scenarios?Memory Errors
  • Freeing unallocated memory
  • When 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 memory
  • When does this happen in real-world scenarios?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?
  • When does this happen in real-world scenarios?Memory Errors
  • Unitialized memory read
  • When does this happen in real-world scenarios?Outline
  • Announcements/Review
  • Grade project 2D
  • Building Large Projects
  • Mangling with C/C++
  • Project 3A
  • Background on images
  • Bonus topics
  • Memory Errors
  • Webpages
  • Web pages
  • ssh –l <user name> ix.cs.uoregon.edu
  • cd public_html
  • put something in index.html
  •  it will show up as
  • http://ix.cs.uoregon.edu/~<username>Web pages
  • You can also exchange files this way
  • scpfile.pdf <username>@ix.cs.uoregon.edu:~/public_html
  • point people to http://ix.cs.uoregon.edu/~<username>/file.pdf
  • Note that ~/public_html/dir1shows up as http://ix.cs.uoregon.edu/~<username>/dir1(“~/dir1” is not accessible via web)
    Search Related
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks