Lecture 7 of CIS 330, presented by Hank Childs from the University of Oregon on April 23, 2014, delves into memory manipulation and managing large projects. Key topics include memory segments, the von Neumann architecture, pointers, and practical tips for creating effective project structures. The lecture also covers upcoming projects, including Project 2N, 2C, and 2D, as well as essential announcements regarding schedules and office hours. Students are encouraged to participate in the Luks Programming Contest and focus on building reproducible coding environments for better debugging.
01 / 59Memory Manipulation and Project Structures in CIS 330 - Lecture 7 Overview
1 / 17
Télécharger la présentation
Memory Manipulation and Project Structures in CIS 330 - Lecture 7 Overview
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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.
Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects
Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects
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
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
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?
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
“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.
Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects
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
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;
Pointers • Pointers store locations in memory printf prints pointers with “%p”
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
‘*’ operator • Let “ptr” be a pointer • Then “*ptr” returns value in the address that pts points to. • * = “dereference operator”
Behavior of dereference • When you dereference, you get the value at that moment. • Whatever happens afterwards won’t have effect.
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);
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
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
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.
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
Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects
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.
Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects
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.
Outline • Announcements/Review • Project 2N • Memory and Pointers • Project 2C • Project 2D • Building Large Projects
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?
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