1 / 58

Hank Childs, University of Oregon

egan
Download Presentation

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 8: Building Large Projects, Mangling and Namespaces Beginning on Project 3 Hank Childs, University of Oregon April 25th, 2014

  2. Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages

  3. Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages

  4. 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

  5. Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages

  6. 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.

  7. Pointers • Pointers store locations in memory • “&”: unary operator that gives the address of a variable. int x; int *yp = &x;

  8. ‘*’ operator • Let “ptr” be a pointer • Then “*ptr” returns value in the address that ptr points to. • * = “dereference operator”

  9. Pointer Arithmetic • You can combine pointers and integers to get new pointer locations ptr + 1 ptr + sizeof(type) bytes

  10. 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.

  11. [ ] operator • [ ] is a way of dereferencing memory • Recall that ‘*’ is the dereference operator • A[0] <= => *A • A[5] <= => *(A+5);

  12. 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

  13. 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

  14. Questions? • Something unclear from last time? • Questions about HW? • vi tips?

  15. Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages

  16. Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages

  17. 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); }

  18. 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.

  19. 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

  20. 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?

  21. 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

  22. 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); }

  23. 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.

  24. 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

  25. Preprocessor Phases • Resolve #includes • (we understand #include phase) • Conditional compilation • Macro replacement • Special macros

  26. #define compilation This is an example of macro replacement.

  27. #define via gcc command-line option

  28. Conflicting –D and #define

  29. Conditional compilation

  30. Conditional compilation controlled via compiler flags This is how configure/cmake controls the compilation.

  31. 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?

  32. Compilation error

  33. gcc –E rectangle.c

  34. #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++.

  35. 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__

  36. Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages

  37. Problem with C…

  38. Problem with C… No checking of type…

  39. Problem is fixed with C++…

  40. Problem is fixed with C++…

  41. 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++.

  42. C++ will let you overload functions with different types

  43. C++ also gives you access to mangling via “namespaces” Functions or variables within a namespace are accessed with “::”

  44. 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.

  45. Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages

  46. 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

  47. 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)

  48. 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)

  49. Outline • Announcements/Review • Grade project 2D • Building Large Projects • Mangling with C/C++ • Project 3A • Background on images • Bonus topics • Memory Errors • Webpages

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

More Related