DOC PREVIEW
Berkeley COMPSCI 61C - Introduction to C

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 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 28 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 28 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 28 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 28 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 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61C L05 Introduction to C (pt 3) (1)!Garcia, Spring 2010 © UCB!! !Lecturer SOE Dan Garcia!! !www.cs.berkeley.edu/~ddgarcia inst.eecs.berkeley.edu/~cs61c "CS61C : Machine Structures" Lecture 5 – Introduction to C (pt 3)"C Memory Management" 2010-01-29!Appleʼs iPad, day 2 ⇒"After the dust has settled," what do we have? Name causes chuckles & lawsuits (Fujitsu). “Haters” say nothing new, closed system. !apple.com/ipad!CS61C L05 Introduction to C (pt 3) (2)!Garcia, Spring 2010 © UCB!Review!• Pointers and arrays are virtually same!• C knows how to increment pointers!• C is an efficient language, with little protection!• Array bounds not checked!• Variables not automatically initialized!• (Beware) The cost of efficiency is more overhead for the programmer.!• “C gives you a lot of extra rope but be careful not to hang yourself with it!”!CS61C L05 Introduction to C (pt 3) (3)!Garcia, Spring 2010 © UCB!Pointers (1/4)!• Sometimes you want to have a procedure increment a variable?!• What gets printed?!void AddOne(int x) { x = x + 1; } int y = 5; AddOne( y); printf(“y = %d\n”, y); y = 5 …review…!CS61C L05 Introduction to C (pt 3) (4)!Garcia, Spring 2010 © UCB!Pointers (2/4)!• Solved by passing in a pointer to our subroutine.!• Now what gets printed?!void AddOne(int *p) { *p = *p + 1; } int y = 5; AddOne(&y); printf(“y = %d\n”, y); y = 6 …review…!CS61C L05 Introduction to C (pt 3) (5)!Garcia, Spring 2010 © UCB!Pointers (3/4)!• But what if what you want changed is a pointer?!• What gets printed?!void IncrementPtr(int *p) { p = p + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr( q); printf(“*q = %d\n”, *q); *q = 50 50 60 70 A qCS61C L05 Introduction to C (pt 3) (6)!Garcia, Spring 2010 © UCB!Pointers (4/4)!• Solution! Pass a pointer to a pointer, declared as **h!• Now what gets printed?!void IncrementPtr(int **h) { *h = *h + 1; } int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q); printf(“*q = %d\n”, *q); *q = 60 50 60 70 A q qCS61C L05 Introduction to C (pt 3) (7)!Garcia, Spring 2010 © UCB!Dynamic Memory Allocation (1/4)!• C has operator sizeof() which gives size in bytes (of type or variable)!• Assume size of objects can be misleading and is bad style, so use sizeof(type)!• Many years ago an int was 16 bits, and programs were written with this assumption. !• What is the size of integers now?!• “sizeof” knows the size of arrays:!int ar[3]; // Or: int ar[] = {54, 47, 99}!sizeof(ar) ⇒ 12!• …as well for arrays whose size is determined at run-time:!int n = 3;!int ar[n]; // Or: int ar[fun_that_returns_3()];!sizeof(ar) ⇒ 12!CS61C L05 Introduction to C (pt 3) (8)!Garcia, Spring 2010 © UCB!Dynamic Memory Allocation (2/4)!• To allocate room for something new to point to, use malloc() (with the help of a typecast and sizeof):"ptr = (int *) malloc (sizeof(int));!• Now, ptr points to a space somewhere in memory of size (sizeof(int)) in bytes.!• (int *) simply tells the compiler what will go into that space (called a typecast).!• malloc is almost never used for 1 var!ptr = (int *) malloc (n*sizeof(int));!• This allocates an array of n integers.!CS61C L05 Introduction to C (pt 3) (9)!Garcia, Spring 2010 © UCB!Dynamic Memory Allocation (3/4)!• Once malloc() is called, the memory location contains garbage, so donʼt use it until youʼve set its value.!• After dynamically allocating space, we must dynamically free it:!free(ptr);!• Use this command to clean up.!• Even though the program frees all memory on exit (or when main returns), donʼt be lazy!!• You never know when your main will get transformed into a subroutine!!CS61C L05 Introduction to C (pt 3) (10)!Garcia, Spring 2010 © UCB!Dynamic Memory Allocation (4/4)!• The following two things will cause your program to crash or behave strangely later on, and cause VERY VERY hard to figure out bugs:!• free()ing the same piece of memory twice!• calling free() on something you didnʼt get back from malloc() !• The runtime does not check for these mistakes!• Memory allocation is so performance-critical that there just isnʼt time to do this !• The usual result is that you corrupt the memory allocatorʼs internal structure!• You wonʼt find out until much later on, in a totally unrelated part of your code!!CS61C L05 Introduction to C (pt 3) (11)!Garcia, Spring 2010 © UCB!Arrays not implemented as youʼd think!void foo() { int *p, *q, x; int a[4]; p = (int *) malloc (sizeof(int)); q = &x; *p = 1; // p[0] would also work here printf("*p:%u, p:%u, &p:%u\n", *p, p, &p); *q = 2; // q[0] would also work here printf("*q:%u, q:%u, &q:%u\n", *q, q, &q); *a = 3; // a[0] would also work here printf("*a:%u, a:%u, &a:%u\n", *a, a, &a); } ?" ?" ..." ..." 0 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 ..."p q x"?" ?" ?"unnamed-malloc-space"40" 20" 2" 3" 1"*p:1, p:40, &p:12 *q:2, q:20, &q:16 *a:3, a:24, &a:24 K&R: “An array name is not a variable”!a"24"?"CS61C L05 Introduction to C (pt 3) (12)!Garcia, Spring 2010 © UCB!Binky Pointer Video (thanks to NP @ SU)!CS61C L05 Introduction to C (pt 3) (13)!Garcia, Spring 2010 © UCB!Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta!1. Kid meets giant Texas people exercising zen-like yoga. – Rolf O 2. Kind men give ten percent extra, zestfully, youthfully. – Hava E 3. Kissing Mentors Gives Testy Persistent Extremists Zealous Youthfulness. – Gary M 4. Kindness means giving, teaching, permeating excess zeal yourself. – Hava E 5. Killing messengers gives terrible people exactly zero, yo 6. Kindergarten means giving teachers perfect examples (of) zeal (&) youth 7. Kissing mediocre girls/guys teaches people (to) expect zero (from) you 8. Kinky Mean Girls Teach Penis-Extending Zen Yoga 9. Kissing Mel Gibson, Tom Petty exclaimed: “Zesty, yo!” – Dan G 10. Kissing me gives ten percent extra zeal & youth! – Dan G (borrowing parts)CS61C L05 Introduction to C (pt 3) (14)!Garcia, Spring 2010 © UCB!Which are guaranteed to print out 5? !I: main() { " int *a-ptr = (int *)malloc(int); " *a-ptr = 5; " printf(“%d”, *a-ptr);" }!II:main() {" int *p, a = 5; " p = &a; ..." /* code; a,p NEVER on LEFT of = */ " printf(“%d”, a); " }!Peer Instruction! I II "a) - -"b) - YES"c)


View Full Document

Berkeley COMPSCI 61C - Introduction to C

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download Introduction to C
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 Introduction to C 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 Introduction to C 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?