1 / 59

Hank Childs, University of Oregon

howell
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 7: Manipulating Memory & Building Large Projects Hank Childs, University of Oregon April 23rd, 2014

  2. Two experiences from my past • Math 108: An Introduction to Advanced Mathematics • ECS 110: Data Structures I know this is tough … for you and me both. I truly believe that this class is a springboard to success as a computer scientist.

  3. Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects

  4. Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects

  5. Schedule changes for this week • Lecture #7: right now • Lecture #8: Friday 2-3:20 • Lecture #9: Friday 10-11, 11-12, and 4-5 • We will do Proj2N for lecture #9 (and not today) • No lecture on May 9th • (we are banking an extra this week) • Proj 2D: assigned today, due Friday • Will be graded in class • Proj 2C: due Monday

  6. Announcements • Matt OH: Mon/Tues 4-5:30, DES 232 • Pavel OH: Weds/Thurs 4-5, DES 100 • This week only: Thurs (DES 301), cancelled Weds • Hank OH: Thurs 11-12, Fri 10-11, 12:30-1:30 Please come to OH

  7. Luks Programming Contest • 18th Annual Luks Programming Contest • April 19th, 10am-2pm • Deschutes Room 100 • Benefits • Fun • Food • Experience • T-shirt • Extra credit for CIS 330 (2%) 4 CIS 330 attendees … right?

  8. Volunteers Needed • Project 3 is coming soon • Image processing pipeline • We will need to read/write PNG image files • This will involve installing libraries • Need student presentations on package managers • Ideally 5 minute presentation/show-n-tell for multiple package managers Each presenter will earn 2% extra credit

  9. “Reproducers” • Very hard to debug your problems with partial information • Just a compiler error • Just one of several source files • … leads to extra iterations and extra work for me • Reproducer: a self-contained environment that reproduces your problem. • Ideally: • all your source code • a Makefile • the compile error message or output from the code that is problematic Please do your best to send me good reproducers. This is good practice for later in your career.

  10. Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects

  11. Memory Segments • Von Neumann architecture: one memory space, for both instructions and data •  so break memory into “segments” • … creates boundaries to prevent confusion • 4 segments: • Code segment • Data segment • Stack segment • Heap segment

  12. Stack vs Heap: Pros and Cons Lots more to this lecture … please review slides if you missed it

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

  14. Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects

  15. Project 2N lecture being held during Friday’s discussion session.

  16. Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects

  17. Hexadecimal • Binary: 2 values • Decimal: 10 values • Hexadecimal: 16 values • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F • 0x: prefix for hexadecimal • 0x10 = 16 • 0x101 = 257

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

  19. Pointers • Pointers store locations in memory

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

  21. Pointers • Pointers store locations in memory printf prints pointers with “%p”

  22. NULL pointer • NULL: defined by compiler to be a location that is not valid. • Typically 0x00000000 • You can use NULL to initialize pointers, and also to check to see whether a pointer is set already. IBM team I worked on used 0xDEADBEEF, not NULL

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

  24. Behavior of dereference • When you dereference, you get the value at that moment. • Whatever happens afterwards won’t have effect.

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

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

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

  28. More array relationships int A[10]; int *B; B=(A+5)  A[5] = B[0] B=&(A[0])  B = A B=&(A[5])  B = A+5

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

  30. Simple pointers to pointers example

  31. What’s the difference between these two programs? Answer: X is on the heap on the left, and on the stack on the right. But they are both pointers-to-pointers.

  32. What’s the difference between these two programs? Answer: program on left makes one allocation for each pointer, program on right makes one allocation for whole program & each pointer points at locations within that allocation.

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

  34. Call by Reference

  35. Call by Value

  36. Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects

  37. Project 2C Monday April 28th This project will be hard for some of you. I encourage you to start early. I will also try to schedule extra OH for Monday.

  38. Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects

  39. Project 2D • Worksheet of memory manipulation problems • Due Friday 2pm (<48 hours from now) • We will grade them (and discuss them) in class • The results will be collected and be part of your grade • If you can’t make class, let me know ASAP and make arrangements to submit ahead of time.

  40. Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects

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

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

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

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

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

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

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

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

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

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

More Related