CS61C : Machine Structures

Similar documents
CS61C : Machine Structures

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

CS61C : Machine Structures

review Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection

CS61C : Machine Structures

CS61C : Machine Structures

ECE 15B COMPUTER ORGANIZATION

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

Lectures 13 & 14. memory management

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Has there been an update to ANSI C? Lecture #3 C Pointers Yes! It s called the C99 or C9x std

CS61C : Machine Structures

HW1 due Monday by 9:30am Assignment online, submission details to come

review Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

CS 61c: Great Ideas in Computer Architecture

C Arrays, Strings, More Pointers Instructor: Steven Ho

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

More C Pointer Dangers

Functions:"" Breaking"large"computing"tasks"to" smaller"ones"

CS61C : Machine Structures

Numbers: positional notation. CS61C Machine Structures. Faux Midterm Review Jaein Jeong Cheng Tien Ee. www-inst.eecs.berkeley.

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #4. Agenda

IT 252 Computer Organization and Architecture Introduction to the C Programming Language

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

Intermediate Programming, Spring 2017*

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C : Machine Structures

Intro to C: Pointers and Arrays

CS24 Week 2 Lecture 1

Review Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection

Character Strings. String-copy Example

Lecture 7: Procedures and Program Execution Preview

Introduction to Computer Architecture

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Dynamic Data Structures. CSCI 112: Programming in C

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

In Java we have the keyword null, which is the value of an uninitialized reference type

Pointers, Arrays, Memory: AKA the cause of those Segfaults

CS61C : Machine Structures

Fall 2018 Discussion 2: September 3, 2018

Heap Arrays and Linked Lists. Steven R. Bagley

CS 11 C track: lecture 5

Understanding Pointers

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

Lab 3. Pointers Programming Lab (Using C) XU Silei

CS 222: Pointers and Manual Memory Management

Arrays and Pointers in C. Alan L. Cox

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CS61, Fall 2012 Section 2 Notes

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

CSC 1600 Memory Layout for Unix Processes"

Systems Programming and Computer Architecture ( )

SYSC 2006 C Winter 2012

Review! Lecture 5 C Memory Management !

Lecture 2: C Programm

Sir Syed University of Engineering and Technology. Computer Programming & Problem Solving ( CPPS ) Pointers. Chapter No 7

CS61C : Machine Structures

Arrays and Memory Management

CS61C : Machine Structures

Agenda. Pointer Arithmetic. Pointer Arithmetic pointer + number, pointer number 6/23/2011. Pointer Arithmetic: Peer Instruction Question

Memory Allocation. General Questions

Heap Arrays. Steven R. Bagley

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Arrays, Pointers and Memory Management

Data Representation and Storage. Some definitions (in C)

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Arrays and Pointers (part 1)

First of all, it is a variable, just like other variables you studied

Lecture 10: Program Development versus Execution Environment

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

ME964 High Performance Computing for Engineering Applications

CS61C : Machine Structures

Arrays and Pointers (part 1)

Pointers, Dynamic Data, and Reference Types

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

Pointers in C/C++ 1 Memory Addresses 2

Memory, Data, & Addressing II CSE 351 Spring

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II

Transcription:

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia CS61C L05 C Structures, Memory Management (1) 2005-01-28 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. www.cnn.com/2005/tech/01/27/musics.future.ap/

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 (2)

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 (3)

Pointers to pointers (1/4) review Sometimes you want to have a procedure increment a variable? What gets printed? void AddOne(int x) { x = x + 1; } y = 5 int y = 5; AddOne( y); printf( y = %d\n, y); CS61C L05 C Structures, Memory Management (4)

Pointers to pointers (2/4) review Solved by passing in a pointer to our subroutine. Now what gets printed? void AddOne(int *p) { *p = *p + 1; } y = 6 int y = 5; AddOne(&y); printf( y = %d\n, y); CS61C L05 C Structures, Memory Management (5)

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; } A q *q = 50 int A[3] = {50, 60, 70}; int *q = A; IncrementPtr( q); printf( *q = %d\n, *q); 50 60 70 CS61C L05 C Structures, Memory Management (6)

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; } A q *q = 60 q int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q); printf( *q = %d\n, *q); 50 60 70 CS61C L05 C Structures, Memory Management (7)

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 (8)

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 (9)

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 (10)

Binky Pointer Video (thanks to NP @ SU) CS61C L05 C Structures, Memory Management (11)

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 (12)

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 (13)

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 (14)

Peer Instruction 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); } CS61C L05 C Structures, Memory Management (15) I II III 1: - - - 2: - - YES 3: - YES - 4: - YES YES 5: YES - - 6: YES - YES 7: YES YES - 8: YES YES YES

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 (16)

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; } list: node:? NULL string: abc CS61C L05 C Structures, Memory Management (17)

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; } list: node:? NULL string:? abc CS61C L05 C Structures, Memory Management (18)

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; } list: node:????? string: abc NULL CS61C L05 C Structures, Memory Management (19)

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; } list: node:? abc string: abc NULL CS61C L05 C Structures, Memory Management (20)

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; } list: node: abc string: abc NULL CS61C L05 C Structures, Memory Management (21)

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 (22)

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 (23)

Peer Instruction int main(void){ int A[] = {5,10}; int *p = A; 5 10 A[1] A[0] p 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 two printf output? 1: 101 10 5 10 then 101 11 5 11 2: 104 10 5 10 then 104 11 5 11 3: 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 ERROR 6: I surrender! CS61C L05 C Structures, Memory Management (24)