CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia inst.eecs.berkeley.edu/~cs61c.

Slides:



Advertisements
Similar presentations
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
CS 241 Section Week #2 2/4/10. 2 Topics This Section MP1 overview Part1: Pointer manipulation Part2: Basic dictionary structure implementation Review.
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.
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 L4 C Memory Management (1) Beamer, Summer 2007 © UCB Scott Beamer, Instructor inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4.
CS61C L04 Introduction to C (pt 2) (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
UBC104 Embedded Systems Variables, Structures & Pointers.
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,
CS 61C L06 C Memory Management (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
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.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
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 -
CS61C L04 Introduction to C (pt 2) (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
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.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
CS 61C: Great Ideas in Computer Architecture Introduction to C, Part II Instructors: John Wawrzynek & Vladimir Stojanovic
C Programming Lecture 10-1 : Array & Pointer. Character Array String A sequence of characters The last character should be ‘\0’ that indicates “the end.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
CS 61C L2.1.1 Memory Management (1) K. Meinz, Summer 2004 © UCB CS61C : Machine Structures Lecture Memory Management Kurt Meinz inst.eecs.berkeley.edu/~cs61c.
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:
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Cs641 pointers/numbers.1 Overview °Stack and the Heap °malloc() and free() °Pointers °numbers in binary.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
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.
CSC Programming for Science Lecture 34: Dynamic Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 16 Pointers and Arrays Pointers and Arrays We've seen examples of both of these in our LC-3 programs; now we'll see them in C. Pointer Address.
What do I need to Know For My Assignment?. C Pointer Review To declare a pointer, we use the * operator. This is similar to but different from using *
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CS61C L04 Introduction to C (pt 2) (1) Garcia, Spring 2010 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C.
March, 2006 Saeid Nooshabadi
Dynamic Memory Allocation
CSC 253 Lecture 8.
Instructor Paul Pearce
CSC 253 Lecture 8.
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.
CS61C - Machine Structures Lecture 4 C Structures Memory Management
Hello to Scott Sloan from Superior, WI!
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.
Chapter 16 Pointers and Arrays
C++ Pointers and Strings
Presentation transcript:

CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 – C Memory Management The future of music!   They now have a surround format for MP3, with players to give the “surround” effect while you walk around in headphones! Walk right and it’ll be louder in your right ear…cool.

CS61C L05 C Structures, Memory Management (2) Garcia, Spring 2005 © UCB Pointer Arithmetic Summary x = *(p+1) ?  x = *(p+1) ; x = *p+1 ?  x = (*p) + 1 ; x = (*p)++ ?  x = *p ; *p = *p + 1; x = *p++ ? (*p++) ? *(p)++ ? *(p++) ?  x = *p ; p = p + 1; x = *++p ?  p = p + 1 ; x = *p ; Lesson? These cause more problems than they solve!

CS61C L05 C Structures, Memory Management (3) Garcia, Spring 2005 © UCB C String Standard Functions int strlen(char *string); compute the length of string int strcmp(char *str1, char *str2); return 0 if str1 and str2 are identical (how is this different from str1 == str2 ?) char *strcpy(char *dst, char *src); copy the contents of string src to the memory at dst. The caller must ensure that dst has enough memory to hold the data to be copied.

CS61C L05 C Structures, Memory Management (4) Garcia, Spring 2005 © UCB Pointers to 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 C Structures, Memory Management (5) Garcia, Spring 2005 © UCB Pointers to 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 C Structures, Memory Management (6) Garcia, Spring 2005 © UCB Pointers to 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 = A q

CS61C L05 C Structures, Memory Management (7) Garcia, Spring 2005 © UCB Pointers to pointers (4/4) Solution! Pass a pointer to a pointer, called a handle, 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 = A q q

CS61C L05 C Structures, Memory Management (8) Garcia, Spring 2005 © UCB Dynamic Memory Allocation (1/3) C has operator sizeof() which gives size in bytes (of type or variable) Assume size of objects can be misleading & is bad style, so use sizeof(type) Many years ago an int was 16 bits, and programs assumed it was 2 bytes

CS61C L05 C Structures, Memory Management (9) Garcia, Spring 2005 © UCB Dynamic Memory Allocation (2/3) 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 C Structures, Memory Management (10) Garcia, Spring 2005 © UCB Dynamic Memory Allocation (3/3) 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.

CS61C L05 C Structures, Memory Management (11) Garcia, Spring 2005 © UCB Binky Pointer Video (thanks to SU)

CS61C L05 C Structures, Memory Management (12) Garcia, Spring 2005 © UCB C structures : Overview A struct is a data structure composed for simpler data types. Like a class in Java/C++ but without methods or inheritance. struct point { int x; int y; }; void PrintPoint(struct point p) { printf(“(%d,%d)”, p.x, p.y); }

CS61C L05 C Structures, Memory Management (13) Garcia, Spring 2005 © UCB C structures: Pointers to them The C arrow operator ( -> ) dereferences and extracts a structure field with a single operator. The following are equivalent: struct point *p; printf(“x is %d\n”, (*p).x); printf(“x is %d\n”, p->x);

CS61C L05 C Structures, Memory Management (14) Garcia, Spring 2005 © UCB 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

CS61C L05 C Structures, Memory Management (15) Garcia, Spring 2005 © UCB Which are guaranteed to print out 5? I: main() { int *a-ptr; *a-ptr = 5; printf(“%d”, *a-ptr); } II: main() { int *p, a = 5; p = &a;... /* code; a & p NEVER on LHS of = */ printf(“%d”, a); } III: main() { int *ptr; ptr = (int *) malloc (sizeof(int)); *ptr = 5; printf(“%d”, *ptr); } Peer Instruction I II III 1: : - - YES 3: - YES - 4: - YES YES 5: YES - - 6: YES - YES 7: YES YES - 8: YES YES YES

CS61C L05 C Structures, Memory Management (16) Garcia, Spring 2005 © UCB Bonus: Linked List Example Let’s look at an example of using structures, pointers, malloc(), and free() to implement a linked list of strings. struct Node { char *value; struct Node *next; }; typedef struct Node *List; /* Create a new (empty) list */ List ListNew(void) { return NULL; }

CS61C L05 C Structures, Memory Management (17) Garcia, Spring 2005 © UCB Linked List Example /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } node: list: string: “abc” …… NULL ?

CS61C L05 C Structures, Memory Management (18) Garcia, Spring 2005 © UCB Linked List Example /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } node: list: string: “abc” …… NULL ? ?

CS61C L05 C Structures, Memory Management (19) Garcia, Spring 2005 © UCB Linked List Example /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } node: list: string: “abc” …… NULL ? “????”

CS61C L05 C Structures, Memory Management (20) Garcia, Spring 2005 © UCB Linked List Example /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } node: list: string: “abc” …… NULL ? “abc”

CS61C L05 C Structures, Memory Management (21) Garcia, Spring 2005 © UCB Linked List Example /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } node: list: string: “abc” …… NULL “abc”

CS61C L05 C Structures, Memory Management (22) Garcia, Spring 2005 © UCB Linked List Example /* add a string to an existing list */ List list_add(List list, char *string) { struct Node *node = (struct Node*) malloc(sizeof(struct Node)); node->value = (char*) malloc(strlen(string) + 1); strcpy(node->value, string); node->next = list; return node; } node: …… NULL “abc”

CS61C L05 C Structures, Memory Management (23) Garcia, Spring 2005 © UCB “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.

CS61C L05 C Structures, Memory Management (24) Garcia, Spring 2005 © UCB int main(void){ int A[] = {5,10}; int *p = A; printf(“%u %d %d %d\n”,p,*p,A[0],A[1]); p = p + 1; printf(“%u %d %d %d\n”,p,*p,A[0],A[1]); *p = *p + 1; printf(“%u %d %d %d\n”,p,*p,A[0],A[1]); } If the first printf outputs , what will the other two printf output? 1: then : then : then 101 4: then 104 5: One of the two printfs causes an ERROR 6: I surrender! Peer Instruction A[1] 510 A[0]p