DOC PREVIEW
UW CSE 303 - Lecture Notes

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

10/21/20091David Notkin  Autumn 2009  CSE303 Lecture 10"If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL." http://www.gdargaud.net/Humor/C_Prog_Debug.htmlToday• Some C leftovers from Monday• C memory model; stack allocation– computer memory and addressing– stack vs. heap– pointers– parameter passing• by value• by referenceCSE303 Au09 2Arrays as parameters• Arrays do not know their own size; they are just memory chunks – harder than in Javaint 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 sizeint 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 parameterint 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 returnedint[] copy(int a[], int size);int main(void) {int numbers[5] = {7, 4, 3, 15, 2};int numbers2[5] = copy(numbers, 5); // noreturn 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}10/21/20092Solution: output parameter• workaround: create the return array outside and pass it in -- "output parameter" works because arrays are passed by referencevoid 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"; // noint answer = 42;printf("The answer is " + answer); // noint len = "hello".length(); // noint printMessage(String s, int times) { ... // noMemory hierarchyCPU registersL1/L2 cache (on CPU)physical RAM (memory)a few bytes1-4 MBvirtual RAM (on a hard disk)secondary/permanent storage(hard disks, removable drives, network)1-2 GB2-8 GB500 GBVirtual 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 addressesrealityprocess 1process 2virtualProcess memory layout• when a process runs, its instructions/globalsload into memory• address space is like a huge array of bytes– total: 232bytes– each int = 4 bytes• as functions are called, data goes on a stack• dynamic data is created on a heapstack(function calls)available memoryheap(dynamically allocated data)global/static variables("data segment")code instructions("text segment")0x000000000xFFFFFFFFaddressspaceStack frames• stack frame or activation record: memory for a function call– stores parameters, local variables, and return address to go back toint f(int p1, int p2) {int x;int a[3];...return x + y;}stackavailableheapglobal datacodeoffset contents24 p220 p116 return address12 a[2]8 a[1]4 a[0]0 x10/21/20093gpfp1, p2x, aTracing function callsint 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;}stackavailableheapglobal datacodemainfgmainn1gpThe & operator&variable produces variable's memory address#include <stdio.h>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 0x0022ff8cprintf("y is at %p\n", &y); // y is at 0x0022ff88printf("a[0] is at %p\n", &a[0]); // a[0] is at 0x0022ff80printf("a[1] is at %p\n", &a[1]); // a[1] is at 0x0022ff84return 0;}– %p placeholder in printf prints a memory address in hexadecimalDanger!• array bounds are not enforced; can overwrite othervariables#include <stdio.h>int main(void) {int x = 10, y = 20;int a[2] = {30, 40};printf("x = %d, y = %d\n", x, y); // x = 10, y = 20a[2] = 999; // !!!a[3] = 111; // !!!printf("x = %d, y = %d\n", x, y); // x = 111, y = 999return 0;}Segfault• segmentation fault ("segfault"): A program crash caused by an attempt to access an illegal area of memory#include <stdio.h>int main(void) {int a[2];a[999999] = 12345; //out of boundsreturn 0;}Segfault#include <stdio.h>void f() {f();}int main(void) {f();return 0;}CSE303 Au09 17The sizeof operatorsizeof(type) or (variable) returns memory size in bytes#include <stdio.h>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=8x uses 4 bytesa uses 20 bytesa[0] uses 4 bytes10/21/20094sizeof continued• arrays passed as parameters do not remember their size#include <stdio.h>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 bytesa uses 4 bytes in fPointer: a memory address referring to another valuetype* name; // declaretype* name = address; // declare/initializeint x = 42;int* p;p = &x; // p stores address of xprintf("x is %d\n", x); // x is 42printf("&x is %p\n", &x); // &x is 0x0022ff8cprintf("p is %p\n", p); // p is 0x0022ff8cint* p1, p2; // int* p1; int p2;int* p1, *p2; // int* p1; int* p2Dereferencing: access the memory referred to by a pointer*pointer // dereference*pointer = value; // dereference/assignint x = 42;int* p;p = &x; // p stores address of x*p = 99; // go to the int p refers to; set to 99printf("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 42printf("&x is %p\n", &x); // &x


View Full Document

UW CSE 303 - Lecture Notes

Documents in this Course
Profiling

Profiling

11 pages

Profiling

Profiling

22 pages

Profiling

Profiling

11 pages

Testing

Testing

12 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?