David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."

Slides:



Advertisements
Similar presentations
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Advertisements

1 Memory Allocation Professor Jennifer Rexford COS 217.
Kernighan/Ritchie: Kelley/Pohl:
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
David Notkin Autumn 2009 CSE303 Lecture 9 Dictionary.com, "c," in The American Heritage® Abbreviations Dictionary, Third Edition. Source location: Houghton.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers Ethan Cerami Fundamentals of Computer New York University.
Introduction to C Programming CE
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
C and Data Structures Baojian Hua
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Memory Layout C and Data Structures Baojian Hua
Computer Science 210 Computer Organization Pointers.
Arrays and Pointers in C Alan L. Cox
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Pointers CSE 2451 Rong Shi.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Runtime Environments Compiler Construction Chapter 7.
Stack and Heap Memory Stack resident variables include:
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
1 Program Layout in memory –Code –Data Global variables –Stack Local variables & function Invocation Frames –Heap Dynamically allocated memory Today’s.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
Object-Oriented Programming in C++
Dynamic memory allocation and Pointers Lecture 4.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Pointers *, &, array similarities, functions, sizeof.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CS1010 Programming Methodology
CSE 220 – C Programming Pointers.
CSE 303 Lecture 9 C programming: types, functions, and arrays
Hassan Khosravi / Geoffrey Tien
Pointers.
Hassan Khosravi / Geoffrey Tien
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
Lecture 6 C++ Programming
INC 161 , CPE 100 Computer Programming
CSC 253 Lecture 8.
CSC215 Lecture Pointers and Arrays.
5. Arrays, Pointers and Strings
CSC 253 Lecture 8.
Memory Allocation CS 217.
Lecture 18 Arrays and Pointer Arithmetic
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Simulating Reference Parameters in C
Lecture 18: Deep C Garbage Collection CS201j: Engineering Software
Memory and Arrays CSE 333 Summer 2018
C (and C++) Pointers April 4, 2019.
Pointers Pointers point to memory locations
CSE 303 Lecture 10 C memory model; stack allocation
Memory and Arrays CSE 333 Winter 2019
SPL – PS2 C++ Memory Handling.
Presentation transcript:

David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."

Today Some C leftovers from Monday C memory model; stack allocation –computer memory and addressing –stack vs. heap –pointers –parameter passing by value by reference CSE303 Au092

Arrays as parameters Arrays do not know their own size; they are just memory chunks – harder than in Java int sumAll(int a[]); int main(void) { int numbers[5] = {7, 4, 3, 15, 2}; int sum = sumAll(numbers); return 0; } int sumAll(int a[]) { int i, sum = 0; for (i = 0; i <... ??? }

Solution 1: declare size Declare a function with the array's exact size int sumAll(int a[5]); int main(void) { int numbers[5] = {7, 4, 3, 15, 2}; int sum = sumAll(numbers); return 0; } int sumAll(int a[5]) { int i, sum = 0; for (i = 0; i < 5; i++) { sum += i; } return sum; }

Solution 2: pass size Pass the array's size as a parameter int sumAll(int a[], int size); int main(void) { int numbers[5] = {7, 4, 3, 15, 2}; int sum = sumAll(numbers, 5); return 0; } int sumAll(int a[], int size) { int i, sum = 0; for (i = 0; i < size; i++) { sum += i; } return sum; }

Returning an array arrays (so far) disappear at the end of the function: this means they cannot be safely returned int[] copy(int a[], int size); int main(void) { int numbers[5] = {7, 4, 3, 15, 2}; int numbers2[5] = copy(numbers, 5); // no return 0; } int[] copy(int a[], int size) { int i; int a2[size]; for (i = 0; i < size; i++) { a2[i] = a[i]; } return a2; // no }

Solution: output parameter workaround: create the return array outside and pass it in -- "output parameter" works because arrays are passed by reference void copy(int a[], int a2[], int size); int main(void) { int numbers[5] = {7, 4, 3, 15, 2}; int numbers2[5]; copy(numbers, numbers2, 5); return 0; } void copy(int a[], int a2[], int size) { int i; for (i = 0; i < size; i++) { a2[i] = a[i]; }

A bit about strings (more soon) String literals are the same as in Java –printf("Hello, world!\n"); –but there is not actually a String type in C; they are just char[] Strings cannot be made, concatenated, or examined as in Java: String s = "hello"; // no int answer = 42; printf("The answer is " + answer); // no int len = "hello".length(); // no int printMessage(String s, int times) {... // no

Memory hierarchy CPU registers L1/L2 cache (on CPU) physical RAM (memory) a few bytes 1-4 MB virtual RAM (on a hard disk) secondary/permanent storage (hard disks, removable drives, network) 1-2 GB 2-8 GB 500 GB

Virtual addressing each process has its own virtual address space of memory to use –each process doesn't have to worry about memory used by others –OS maps from each process's virtual addresses to physical addresses reality process 1 process 2 virtual

Process memory layout when a process runs, its instructions/globals load into memory address space is like a huge array of bytes –total: 2 32 bytes –each int = 4 bytes as functions are called, data goes on a stack dynamic data is created on a heap stack (function calls) available memory heap (dynamically allocated data) global/static variables ("data segment") code instructions ("text segment") 0x xFFFFFFFF address space

Stack frames stack frame or activation record: memory for a function call –stores parameters, local variables, and return address to go back to int f(int p1, int p2) { int x; int a[3];... return x + y; } stack available heap global data code offsetcontents 24 p2 20 p1 16return address 12 a[2] 8 a[1] 4 a[0] 0 x

gpgp f p1, p2 x, a Tracing function calls int main(void) { int n1 = f(3, -5); n1 = g(n1); } int f(int p1, int p2) { int x; int a[3];... x = g(a[2]); return x + y; } int g(int param) { return param * 2; } stack available heap global data code main f g main n1 gpgp

The & operator &variable produces variable 's memory address #include int main(void) { int x, y; int a[2]; // printf("x is at %d\n", &x); printf("x is at %p\n", &x); // x is at 0x0022ff8c printf("y is at %p\n", &y); // y is at 0x0022ff88 printf("a[0] is at %p\n", &a[0]); // a[0] is at 0x0022ff80 printf("a[1] is at %p\n", &a[1]); // a[1] is at 0x0022ff84 return 0; } –%p placeholder in printf prints a memory address in hexadecimal

Danger! array bounds are not enforced; can overwrite other variables #include int main(void) { int x = 10, y = 20; int a[2] = {30, 40}; printf("x = %d, y = %d\n", x, y); // x = 10, y = 20 a[2] = 999; // !!! a[3] = 111; // !!! printf("x = %d, y = %d\n", x, y); // x = 111, y = 999 return 0; }

Segfault segmentation fault ("segfault"): A program crash caused by an attempt to access an illegal area of memory #include int main(void) { int a[2]; a[999999] = 12345; //out of bounds return 0; }

Segfault #include void f() { f(); } int main(void) { f(); return 0; } CSE303 Au0917

The sizeof operator sizeof(type) or (variable) returns memory size in bytes #include int main(void) { int x; int a[5]; printf("int=%d, double=%d\n", sizeof(int), sizeof(double)); printf("x uses %d bytes\n", sizeof(x)); printf("a uses %d bytes\n", sizeof(a)); printf("a[0] uses %d bytes\n", sizeof(a[0])); return 0; } Output: int=4, double=8 x uses 4 bytes a uses 20 bytes a[0] uses 4 bytes

sizeof continued arrays passed as parameters do not remember their size #include void f(int a[]); int main(void) { int a[5]; printf("a uses %d bytes\n", sizeof(a)); f(a); return 0; } void f(int a[]) { printf("a uses %2d bytes in f\n", sizeof(a)); } Output: a uses 20 bytes a uses 4 bytes in f

Pointer: a memory address referring to another value type* name; // declare type* name = address; // declare/initialize int x = 42; int* p; p = &x; // p stores address of x printf("x is %d\n", x); // x is 42 printf("&x is %p\n", &x); // &x is 0x0022ff8c printf("p is %p\n", p); // p is 0x0022ff8c int* p1, p2; // int* p1; int p2; int* p1, *p2; // int* p1; int* p2

Dereferencing: access the memory referred to by a pointer *pointer // dereference *pointer = value; // dereference/assign int x = 42; int* p; p = &x; // p stores address of x *p = 99; // go to the int p refers to; set to 99 printf("x is %d\n", x); Output: x is 99

* vs. & many students get * and & mixed up –& references (ampersand gets an address) –* dereferences(star follows a pointer) int x = 42; int* y = &x; printf("x is %d \n", x); // x is 42 printf("&x is %p\n", &x); // &x is 0x0022ff8c printf("y is %p\n", y); // y is 0x0022ff8c printf("*y is %d \n", *y); // *y is 42 printf("&y is %p\n", &y); // &y is 0x0022ff88 What is *x ?

L-values and R-values L-value: Suitable for being on left-side of an = assignment -- a valid memory address to store into R-value: Suitable for right-side of an = assignment int x = 42; int* p = &x; L-values : x or *p (store into x ), p (changes what p points to) not &x, &p, *x, *(*p), *12 R-values : x or *p, &x or p, &p not &(&p), &42

Pass-by-value: copy parameters' values Cannot change the original (“actual”) parameter variable int main(void) { int a = 42, b = -7; swap(a, b); printf("a = %d, b = %d\n", a, b); return 0; } void swap(int a, int b) { int temp = a; a = b; b = temp; }

Pass-by-reference: point to parameters Can change the actual parameter variable using the “formal” int main(void) { int a = 42, b = -7; swap(a, b); printf("a = %d, b = %d\n", a, b); return 0; } void swap(int a, int b) { int temp = a; a = b; b = temp; }

Questions? CSE303 Au0926