CS61C : Machine Structures

Similar documents
CS61C : Machine Structures

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.

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

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

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)!

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

Lectures 13 & 14. memory management

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

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

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?!

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

CS61C : Machine Structures

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

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

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

CS 61c: Great Ideas in Computer Architecture

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

CS61C : Machine Structures

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

More C Pointer Dangers

C Arrays, Strings, More Pointers Instructor: Steven Ho

CS61C : Machine Structures

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

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

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

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

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

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

Intro to C: Pointers and Arrays

Intermediate Programming, Spring 2017*

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

CS24 Week 2 Lecture 1

Fall 2018 Discussion 2: September 3, 2018

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

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

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

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

CS61C : Machine Structures

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

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

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

Lecture 7: Procedures and Program Execution Preview

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

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

Heap Arrays and Linked Lists. Steven R. Bagley

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

CS61C : Machine Structures

Arrays and Memory Management

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

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

Dynamic Data Structures. CSCI 112: Programming in C

CS 11 C track: lecture 5

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Number review... Lecture 3 Introduction to the C Programming Language (pt 1) Has there been an update to ANSI C?

Character Strings. String-copy Example

Introduction to Computer Architecture

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

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Heap Arrays. Steven R. Bagley

Pointers in C/C++ 1 Memory Addresses 2

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

Arrays and Pointers in C. Alan L. Cox

CS 222: Pointers and Manual Memory Management

CS61C : Machine Structures

CS61C : Machine Structures

REFERENCES, POINTERS AND STRUCTS

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Agenda. Address vs. Value Consider memory to be a single huge array. Review. Pointer Syntax. Pointers 9/9/12

Understanding Pointers

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

CSC 1600 Memory Layout for Unix Processes"

Lecture 2: C Programm

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

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

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

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

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

ME964 High Performance Computing for Engineering Applications

CS 102 Computer Architecture. Lecture 2: Introduction to C, Part I. Instructor: Sören Schwertfeger.

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6

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

CS61, Fall 2012 Section 2 Notes

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

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output }

Lecture 10: Program Development versus Execution Environment

Recitation #11 Malloc Lab. November 7th, 2017

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II. Control. Consider memory to be a single huge array

CS61C : Machine Structures

Transcription:

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia CS 61C L04 C Structures, Memory Management (1) 2004-09-10 Barry Bonds nears 700! We are witness to perhaps the greatest baseball player of all time, and he plays weekly 10 miles from here! Years from now you ll know where you were when he passed 755.

More from Wednesday s lecture 1. #define macros may go anywhere. Thereafter the name is replaced with the replacement text. It is usually good style to put all #defines at the top so that reordering code doesn t cause bugs. 2. void * pointers used to be char * pointers (before ANSI C). Therefore, partially to maintain compatibility, ++ incrementing a void * pointer via increments it by 1 byte. 3. const type qualifier announces objects are not to be changed. Implementationdependent storage and violation penalty. CS 61C L04 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?) int 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. CS 61C L04 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); CS 61C L04 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); CS 61C L04 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 CS 61C L04 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 CS 61C L04 C Structures, Memory Management (7)

Administrivia One extra credit lab checkoff pt! Sign up to get your lab checked off by the first hour and you will get 1 bonus checkoff point to count toward final grade. (Not 1/300, +1 out of 4 for that lab) CS 61C L04 C Structures, Memory Management (8)

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 CS 61C L04 C Structures, Memory Management (9)

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. CS 61C L04 C Structures, Memory Management (10)

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. CS 61C L04 C Structures, Memory Management (11)

Binky Pointer Video (thanks to NP @ SU) CS 61C L04 C Structures, Memory Management (12)

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(point p) { printf( (%d,%d), p.x, p.y); } CS 61C L04 C Structures, Memory Management (13)

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); CS 61C L04 C Structures, Memory Management (14)

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 CS 61C L04 C Structures, Memory Management (15)

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); } CS 61C L04 C Structures, Memory Management (16) I II III 1: - - - 2: - - YES 3: - YES - 4: - YES YES 5: YES - - 6: YES - YES 7: YES YES - 8: YES YES YES

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 Node *List; /* Create a new (empty) list */ List ListNew(void) { return NULL; } CS 61C L04 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 CS 61C L04 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:? NULL string:? abc CS 61C L04 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:????? string: abc NULL CS 61C L04 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 CS 61C L04 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; } list: node: abc string: abc NULL CS 61C L04 C Structures, Memory Management (22)

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 CS 61C L04 C Structures, Memory Management (23)

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. CS 61C L04 C Structures, Memory Management (24)

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! CS 61C L04 C Structures, Memory Management (25)