Instructor Paul Pearce

Slides:



Advertisements
Similar presentations
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Advertisements

Kernighan/Ritchie: Kelley/Pohl:
CS61C L04 Introduction to C (pt 2) (1) Garcia, Fall 2011 © UCB Reference slides You ARE responsible for the material on these slides (they’re just taken.
CS61C L03 Introduction to C (pt 2) (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L4 C Memory Management (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4.
CS61C L3 C Pointers (1) Garcia, Fall 2005 © UCB Lecturer PSOE, new dad Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS61C L05 Introduction to C (pt 3) (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L3 C Pointers (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 – C Strings,
CS61C L06 C Memory Management (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
CS61C L04 Introduction to C (pt 2) (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L06 C Memory Management (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C :
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CS61CL L02 Dynamic Storage (1) Huddleston, Summer 2009 © UCB Jeremy Huddleston inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #3 -
CS 61C L03 C Arrays (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS61C L05 Introduction to C (pt 3) (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
CS61C L06 C Memory Management (1) Garcia, Fall 2006 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS 61C L03 C Pointers (1)Garcia / Patterson Fall 2002 © UCB CS61C - Machine Structures Lecture 3 C pointers  Dan Garcia (
CS61C L3 C Pointers (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 – More C intro,
CS61CL L01 Introduction (1) Huddleston, Summer 2009 © UCB Jeremy Huddleston inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers.
CS 61C L04 C Pointers (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine.
CS 61C L03 C Arrays (1) A Carle, Summer 2006 © UCB inst.eecs.berkeley.edu/~cs61c/ CS61C : Machine Structures Lecture #3: C Pointers & Arrays
CS 61C L04 C Structures, Memory Management (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L4 C Pointers (1) Chae, Summer 2008 © UCB Albert Chae Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 –C Strings,
Instructor: Justin Hsia 6/26/2013Summer Lecture #31 CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topic 4: C Data Structures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Instructor: Justin Hsia 6/20/2012Summer Lecture #31 CS 61C: Great Ideas in Computer Architecture C Arrays, Strings, More Pointers.
CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver 1.
CS61C L04 Introduction to C (pt 2) (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
Dynamic Allocation in C
Computer Organization and Design Pointers, Arrays and Strings in C
The Beauty and Joy of Computing Lecture #22: Future of Computing
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Computer Science 210 Computer Organization
Pointers and Memory Overview
March, 2006 Saeid Nooshabadi
Dynamic Memory Allocation
CSC 253 Lecture 8.
Object Oriented Programming COP3330 / CGS5409
CSC 253 Lecture 8.
Lecturer SOE Dan Garcia
C help session: Tonight 306 Soda
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 – Introduction to C (pt 3) C Memory Management Lecturer SOE Dan Garcia.
Computer Science 210 Computer Organization
Pointers, Dynamic Data, and Reference Types
Memory Allocation CS 217.
CS61C - Machine Structures Lecture 4 C Structures Memory Management
Hello to Scott Sloan from Superior, WI!
EECE.2160 ECE Application Programming
There is one handout today at the front and back of the room!
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EECE.2160 ECE Application Programming
Homework Continue with K&R Chapter 5 Skipping sections for now
Course Overview PART I: overview material PART II: inside a compiler
C Programming - Lecture 5
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Week 9 - Monday CS222.
Presentation transcript:

Instructor Paul Pearce inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 – Introduction to C (pt 3) Review, Malloc, Structures 2010-06-24 Instructor Paul Pearce Voting machines suffer from C pointer bugs! Professor David Wagner (733 Soda) led an audit of several major voting systems and discovered major bugs, such as this one! .... char name; sprintf(&name, "%s"), get_file()); Install(&name, ...); ... Greet class http://www.cs.berkeley.edu/~daw/talks/vote-lisa08.pdf

Pointers and arrays are virtually same 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!”

…review… 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… 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

But what if what you want changed is a pointer? What gets printed? 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 q A 50 60 70

Solution! Pass a pointer to a pointer, declared as **h 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 q q A 50 60 70

Dynamic Memory Allocation (1/4) C has operator sizeof() which gives size in bytes (of type or variable) Assume size of types 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()];

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.

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!

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!

Binky Pointer Video (thanks to NP @ SU) SHOULD BE DONE AT 10:10

Administrivia Next week is memory management and MIPS! By this point next week, you’ll be able to code in assembly! Lab problems Should be fixed! Reminder, you must get checked off at the start of lab TODAY for Tuesday! C Help Session How was it? HW1 due Friday night! If you aren’t sure about how submitting works, ask in lab!

Check malloc’s return value! malloc() can fail! Requested something too large Out of memory! Random OS Error If malloc fails, NULL is returned Bad: int *p = (int *) malloc (sizeof(int)); *p = 42; // Null pointer dereference? Good: if (p != NULL) { *p = 42; } Every time you call malloc(), you MUST check it’s return value against NULL. NO EXCEPTIONS!

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 ... ... ... 40 ? 20 ? 2 ? 3 ? ? 1 a 24 ? p q x unnamed-malloc-space *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”

Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta Kid meets giant Texas people exercising zen-like yoga. – Rolf O Kind men give ten percent extra, zestfully, youthfully. – Hava E Kissing Mentors Gives Testy Persistent Extremists Zealous Youthfulness. – Gary M Kindness means giving, teaching, permeating excess zeal yourself. – Hava E Killing messengers gives terrible people exactly zero, yo Kindergarten means giving teachers perfect examples (of) zeal (&) youth Kissing mediocre girls/guys teaches people (to) expect zero (from) you Kissing Mel Gibson, Tom Petty exclaimed: “Zesty, yo!” – Dan G Kissing me gives ten percent extra zeal & youth! – Dan G (borrowing parts)

C structures : Overview A struct is a data structure composed from simpler data types. Like a class in Java/C++ but without methods or inheritance. struct point { /* type definition */ int x; int y; }; void PrintPoint(struct point p) { printf(“(%d,%d)”, p.x, p.y); } struct point p1 = {0,10}; /* x=0, y=10 */ PrintPoint(p1); As always in C, the argument is passed by “value” – a copy is made.

C structures: Pointers to them Usually, more efficient to pass a pointer to the struct. The C arrow operator (->) dereferences and extracts a structure field with a single operator. The following are equivalent: struct point *p; /* code to assign to pointer */ printf(“x is %d\n”, (*p).x); printf(“x is %d\n”, p->x);

struct p { char x; int y; }; How big are structs? Recall C operator sizeof() which gives size in bytes (of type or variable) How big is sizeof(p)? struct p { char x; int y; }; 5 bytes? 8 bytes? Compiler may word align integer y

Peer Instruction Which are guaranteed to print out 5? I: main() { int *a_ptr = (int*)malloc(sizeof(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); } Answer: 2: III only I fails for TWO reasons. You can’t use “-” in names of variables and you didn’t allocate int storage! II fails because a subroutine could be called with a pointer to a (e.g., p) which changes a, or someone else could point at a or p and change it. I II a) - - b) - YES c) YES - d) YES YES e) No idea

Use handles to change pointers Create abstractions with structures “And in Conclusion…” Use handles to change pointers Create abstractions with structures Dynamically allocated heap memory must be manually deallocated in C. Use malloc() and free() to allocate and deallocate memory from heap. Have a great weekend!

Reference slides You ARE responsible for the material on these slides (they’re just taken from the reading anyway) ; we’ve moved them to the end and off-stage to give more breathing room to lecture!

Linked List Example Let’s look at an example of using structures, pointers, malloc(), and free() to implement a linked list of strings. /* node structure for linked list */ struct Node { char *value; struct Node *next; }; Recursive definition!

typedef simplifies the code struct Node { char *value; struct Node *next; }; /* "typedef" means define a new type */ typedef struct Node NodeStruct; … OR … typedef struct Node { struct Node *next; } NodeStruct; … THEN typedef NodeStruct *List; typedef char *String; String value; /* Note similarity! */ /* To define 2 nodes */ struct Node { char *value; struct Node *next; } node1, node2;

Linked List Example /* Add a string to an existing list */ List cons(String s, List list) { List node = (List) malloc(sizeof(NodeStruct)); node->value = (String) malloc (strlen(s) + 1); strcpy(node->value, s); node->next = list; return node; } String s1 = "abc", s2 = "cde"; List theList = NULL; theList = cons(s2, theList); theList = cons(s1, theList); /* or, just like (cons s1 (cons s2 nil)) */ theList = cons(s1, cons(s2, NULL));

Linked List Example /* Add a string to an existing list, 2nd call */ List cons(String s, List list) { List node = (List) malloc(sizeof(NodeStruct)); node->value = (String) malloc (strlen(s) + 1); strcpy(node->value, s); node->next = list; return node; } node: list: "abc" … NULL ? s:

Linked List Example /* Add a string to an existing list, 2nd call */ List cons(String s, List list) { List node = (List) malloc(sizeof(NodeStruct)); node->value = (String) malloc (strlen(s) + 1); strcpy(node->value, s); node->next = list; return node; } list: node: … … ? NULL s: ? "abc"

Linked List Example /* Add a string to an existing list, 2nd call */ List cons(String s, List list) { List node = (List) malloc(sizeof(NodeStruct)); node->value = (String) malloc (strlen(s) + 1); strcpy(node->value, s); node->next = list; return node; } list: node: … … NULL s: ? "abc" "????"

Linked List Example /* Add a string to an existing list, 2nd call */ List cons(String s, List list) { List node = (List) malloc(sizeof(NodeStruct)); node->value = (String) malloc (strlen(s) + 1); strcpy(node->value, s); node->next = list; return node; } list: node: … … NULL s: ? "abc" "abc"

Linked List Example /* Add a string to an existing list, 2nd call */ List cons(String s, List list) { List node = (List) malloc(sizeof(NodeStruct)); node->value = (String) malloc (strlen(s) + 1); strcpy(node->value, s); node->next = list; return node; } list: node: … … NULL s: "abc" "abc"

Linked List Example /* Add a string to an existing list, 2nd call */ List cons(String s, List list) { List node = (List) malloc(sizeof(NodeStruct)); node->value = (String) malloc (strlen(s) + 1); strcpy(node->value, s); node->next = list; return node; } node: … … NULL s: "abc" "abc"