unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Similar documents
When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

CS 11 C track: lecture 5

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS61, Fall 2012 Section 2 Notes

We would like to find the value of the right-hand side of this equation. We know that

Understanding Pointers

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

CS C Primer. Tyler Szepesi. January 16, 2013

CSC 1600 Memory Layout for Unix Processes"

Dynamic Data Structures. CSCI 112: Programming in C

Intermediate Programming, Spring 2017*

Common Misunderstandings from Exam 1 Material

COMP 2355 Introduction to Systems Programming

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Final CSE 131B Spring 2004

C Language Summary (Continued)

Systems Programming and Computer Architecture ( )

just a ((somewhat) safer) dialect.

Section Notes - Week 1 (9/17)

PRINCIPLES OF OPERATING SYSTEMS

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Programs in memory. The layout of memory is roughly:

CSE 333 Lecture 2 - arrays, memory, pointers

CS61C : Machine Structures

Dynamic Memory Allocation and Command-line Arguments

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

PROGRAMMAZIONE I A.A. 2017/2018

Machine-Level Programming V: Unions and Memory layout

ECE264 Fall 2013 Exam 3, November 20, 2013

Dynamic memory allocation

TI2725-C, C programming lab, course

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CS 261 Data Structures. Introduction to C Programming

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

Class Information ANNOUCEMENTS

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Lecture 8 Dynamic Memory Allocation

ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

CSE 124 Discussion (10/3) C/C++ Basics

CSE 12 Spring 2016 Week One, Lecture Two

Dynamic memory allocation (malloc)

CS240: Programming in C

ELEC 377 C Programming Tutorial. ELEC Operating Systems

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Memory Corruption 101 From Primitives to Exploit

Processes. Johan Montelius KTH

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

A process. the stack

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Dynamic Data Structures (II)

CSE 333 Lecture 3 - arrays, memory, pointers

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

o Code, executable, and process o Main memory vs. virtual memory

CSE 333 Lecture 2 Memory

CSE 303: Concepts and Tools for Software Development

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Singly linked lists in C.

First of all, it is a variable, just like other variables you studied

CSE 374 Final Exam 3/15/17 Sample Solution. Question 1. (8 points) Suppose we have the following two statements in a C program:

Procedures, Parameters, Values and Variables. Steven R. Bagley

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

EL2310 Scientific Programming

EC 413 Computer Organization

Topics Introduction to Microprocessors

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

CS201 - Lecture 1 The C Programming Language

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CSE 12 Spring 2018 Week One, Lecture Two

Memory and C/C++ modules

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS 322 Operating Systems Practice Midterm Questions

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

ECE 264 Advanced C Programming 2009/03/11

BBM 201 DATA STRUCTURES

Arrays and Pointers in C. Alan L. Cox

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

CSE 333 Midterm Exam Sample Solution 7/28/14

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Lecture 14 Notes. Brent Edmunds

CS113: Lecture 4. Topics: Functions. Function Activation Records

Lectures 5-6: Introduction to C

CS 5513 Entry Quiz. Systems Programming (CS2213/CS3423))

Run Time Environment

Transcription:

Graded assignment 0 will be handed out in section Assignment 1 Not that bad Check your work (run it through the compiler) Factorial Program Prints out ENTERING, LEAVING, and other pointers unsigned char memory[] 2^32 STACK ¼ 0x8000000 0xC0000000 of address space globals function KERNEL code local variables // y.c #include <stdio.h> int main*() { printf( %p\n, %x); gcc -o y y.c ;./y 0x804a020 // y.c #include <stdio.h> int *y = &x; int main*() { printf( %p\n%p\n, &x, y); gcc -o y y.c ;./y 0x804a020 0x804a020 20a00408 vs. 804a020 (LITTLE ENDIAN) union a { unsigned char c[4]; int i; ;

union a x; x.i = 4; //0x00000004 Therefore: x.c[0] = 4; x.c[1] = 0; x.c[2] = 0; x.c[3] = 0; Compiler decides the memory addresses before the program runs Global addresses valid as long as the programs run STATIC DURATION variable Variable allocated for duration of program Examples: Global variables The code that makes up the program Compiler and OS decide addresses before execution AUTOMATIC duration Allocated for the duration of FUNCTION Local variables uintptr_t stacktop; An integer type large enough to hold an address Example with factorial program: Factorial code from before... int factorial(int n) { if(n==0) x = 1; else x = factorial(n-1) * n; return x; Question: When calling factorial in main, what is happening to the variable x? x has automatic duration new x every time the function executes compiler must allocate space for all of a function s variables at the beginning of the function call, and deallocate said space at the function s end We can represent this in C with a struct: struct fn_args_locals { - each argument - each local variable

In the factorial example: struct factorial_args_and_locals { int n; /* the single argument */ /* the single local variable */ Iterative rewrite of old factorial code: We must first create a global unsigned char stack[1000000] and global variable stacktop, then within factorial_start... 1.) Subtract size of the factorial_args_and_locals struct from stacktop 2.) store a factorial_args_and_locals struct in stack at current stacktop position 3.) set argument in said struct to current argument 4.) check for base case (bottoms out at n = 0) if base case is reached, iterate back up through stack setting local variable x of each struct to local variable x from previous struct times argument n within struct else subtract 1 from current argument and goto step 1.) Visual Representation: First stacktop moves down the stack in intervals of sizeof(factorial_args_and_locals); Each struct put into the stack has 1.) int n, which is the argument 2.) int x, which is the local variable *Note when n = 0, x is set to one due to base/special case

Now we move back up the stack by intervals of sizeof(factorial_args_and_locals) setting x = previous x * n for each struct.

Final Code: struct factorial_args_and_locals { int n; ; unsigned char stack[1000000]; int main(int argc, char **argv) { int n = 2; if(argc >= 2) n = strtol(argv[1], 0, 0); fprint(stderr, %d!=, n); uintptr_t stacktop = &stack[1000000]; int first_n; struct factorial_args_and_locals *l; factorial_start: stacktop -= sizeof(struct factorial_args_and_locals); l = (struct factorial_args_and_locals *) stacktop; l->n = n; if (l->n == 0) l->n = 1; else { n = l->n - 1; goto factorial_start; factorial_returns_here:

l->x = x * l_>n; if (l->n!= first_n) { x = l->x; stacktop += sizeof(struct factorial_args_and_locals); l = (struct factorial_args_and_locals *) staktop; goto factorial_returns_here; printf( %d! == %d\n, n, x); Last 3rd of lecture: Why do we call it a stack? Use stack of paper as an analogy. A stack of paper is orderly, and it s easier to access papers on top of the stack (e.g. remove and add papers to top of stack). This similarly applies to the stacks we reference in class. Heap: In class demonstration with program in_dictionary What type of duration does this program use? Static storage duration for its code. Automatic storage duration for its local variables and arguments. Program time to confirm word is in dictionary is proportional to the size of the dictionary. (Finding 2 words took 0.03 seconds) Why is this program running so slow? It reads data in from dictionary file each time (for each word). Why? Because program put dictionary inside function, which means it has automatic storage duration. So each time function returns, everything gets lost. To solve this, we want to create a global variable. e.g. static char dictionary[1000000] Need 3rd class of memory storage that isn t pre-allocated by compiler but can outlast the duration of a function. This illustrates the need for DYNAMIC STORAGE DURATION. Dynamic storage duration: Variables outlast current function Memory used to hold variables is not controlled by the compiler, it s controlled by the user How do we get access to dynamic storage duration variables? Malloc - allocates sz bytes of space w/ dynamic storage duration. Guaranteed to not overlap with anything else that s active in the program. Free - to get rid of an object with dynamic storage duration. Deallocates object at location pointer so it can be used later What s an object of dynamic storage duration for which no pointers exist? Garbage. Where do objects with dynamic storage duration go? The heap. Look at some memory allocation related errors: Test 28.c Where s the error?

int *array = (int *) malloc(10); This allocated 10 bytes of memory, but following lines of code are initializing all 10 elements of array to 0, but ints have 4 bytes, so we haven t allocated enough memory End note: How would you write the following debugging tool? Valgrind - gives feedback as to memory allocation errors