CS61C L4 C M emory Management (1 ) Beamer, Summer 2007 © UCBScott Beamer, Instructorinst.eecs.berkeley.edu/~cs61cCS61C : Machine Structures Lecture #4 – C Memory Management2007-06-28iPhone Comes out Tomorrowwww.apple.com/iphoneCS61C L4 C M emory Management (2 ) Beamer, Summer 2007 © UCBReview• C99 is the update to the ANSI standard• Pointers and arrays are virtually same• C knows how to increment pointers• C is an efficient language, w/little protection• Array bounds not checked• Variables not automatically initialized• (Beware) The cost of efficiency is moreoverhead for the programmer.• “C gives you a lot of extra rope but be carefulnot to hang yourself with it!”• Use handles to change pointers• P. 53 is a precedence table, useful for (e.g.,)•x = ++*p; ⇒ *p = *p + 1 ; x = *p;CS61C L4 C M emory Management (3 ) Beamer, Summer 2007 © UCBBinky Pointer Video (thanks to NP @ SU)CS61C L4 C M emory Management (4 ) Beamer, Summer 2007 © UCBC structures : Overview• A struct is a data structurecomposed for simpler data types.• Like a class in Java/C++ but withoutmethods or inheritance.struct point { int x; int y;};void PrintPoint(struct point p){ printf(“(%d,%d)”, p.x, p.y);}CS61C L4 C M emory Management (5 ) Beamer, Summer 2007 © UCBC structures: Pointers to them• The C arrow operator (->)dereferences and extracts a structurefield 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 L4 C M emory Management (6 ) Beamer, Summer 2007 © UCBHow big are structs?• Recall C operator sizeof() whichgives 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 yCS61C L4 C M emory Management (7 ) Beamer, Summer 2007 © UCBLinked List Example• Let’s look at an example of usingstructures, pointers, malloc(), andfree() to implement a linked list ofstrings.struct Node { char *value; struct Node *next; };typedef struct Node *List;/* Create a new (empty) list */List ListNew(void){ return NULL; }CS61C L4 C M emory Management (8 ) Beamer, Summer 2007 © UCBLinked 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 L4 C M emory Management (9 ) Beamer, Summer 2007 © UCBLinked 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 L4 C M emory Management (1 0) Beamer, Summer 2007 © UCBLinked 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 L4 C M emory Management (1 1) Beamer, Summer 2007 © UCBLinked 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 L4 C M emory Management (1 2) Beamer, Summer 2007 © UCBLinked 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 L4 C M emory Management (1 3) Beamer, Summer 2007 © UCBLinked 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 L4 C M emory Management (1 4) Beamer, Summer 2007 © UCB“And in Semi-Conclusion…”• Use handles to change pointers• Create abstractions with structures• Dynamically allocated heap memorymust be manually deallocated in C.• Use malloc() and free() to allocateand deallocate memory from heap.CS61C L4 C M emory Management (1 5) Beamer, Summer 2007 © UCBWhich 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 III1: - - -2: - - YES3: - YES -4: - YES YES5: YES - -6: YES - YES7: YES YES -8: YES YES YESCS61C L4 C M emory Management (1 6) Beamer, Summer 2007 © UCBint 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 100 5 5 10, what will the other twoprintf output?1: 101 10 5 10 then 101 11 5 112: 104 10 5 10 then 104 11 5 113: 101 <other> 5 10 then 101 <3-others>4: 104 <other> 5 10 then 104 <3-others>5: One of the two printfs causes an ERROR6: I surrender!Peer InstructionA[1]5 10A[0] pCS61C L4 C M emory Management (1 7) Beamer, Summer 2007 © UCBAdministrivia• Assignments• HW1 due 7/1 @ 11:59pm• HW2 due 7/4 @ 11:59pm• No class on 7/4• Another section is in the works• It won’t be official until the last minute• Keep checking the course website• Once known I will email people onwaitlistCS61C L4 C M emory Management (1 8) Beamer, Summer 2007 © UCBWhere is data allocated?• Structure declaration does notallocate memory• Variable declaration does allocatememory• If declare outside a procedure,allocated in static storage• If declare inside procedure,allocated on the stackand freed
View Full Document