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

Similar documents
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: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

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

Lectures 13 & 14. memory management

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

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

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

CS61C : Machine Structures

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

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

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

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

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

CS61C : Machine Structures

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. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

C Arrays, Strings, More Pointers Instructor: Steven Ho

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

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

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!

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

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

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

More C Pointer Dangers

Intermediate Programming, Spring 2017*

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

Lecture 7: Procedures and Program Execution Preview

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

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

Intro to C: Pointers and Arrays

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

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

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 11 C track: lecture 5

Dynamic Data Structures. CSCI 112: Programming in C

Heap Arrays and Linked Lists. Steven R. Bagley

REFERENCES, POINTERS AND STRUCTS

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

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

CS24 Week 2 Lecture 1

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

Character Strings. String-copy Example

CSC 1600 Memory Layout for Unix Processes"

Introduction to Computer Architecture

Lecture 2: C Programm

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

Understanding Pointers

Fall 2018 Discussion 2: September 3, 2018

Heap Arrays. Steven R. Bagley

Arrays and Pointers in C. Alan L. Cox

Lecture 10: Program Development versus Execution Environment

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

EM108 Software Development for Engineers

CS 449 Lecture 3. Variables in memory. Dr. Jack Lange. int x = 4;

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

Lecture 8 Dynamic Memory Allocation

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

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

CS61C : Machine Structures

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

Pointers, Dynamic Data, and Reference Types

Arrays and Memory Management

CS 222: Pointers and Manual Memory Management

Systems Programming and Computer Architecture ( )

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

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

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

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

CSC C69: OPERATING SYSTEMS

Review! Lecture 5 C Memory Management !

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

CS61C : Machine Structures

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

Arrays and Pointers (part 1)

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

Introduction to C: Pointers

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

ME964 High Performance Computing for Engineering Applications

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

Arrays and Pointers (part 1)

Array Initialization

SYSC 2006 C Winter 2012

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

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Pointers in C/C++ 1 Memory Addresses 2

Memory Management. CSC215 Lecture

Transcription:

CS61C Machine Structures Lecture 5 C Structs & Memory Mangement 1/27/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L05 C Structs (1) 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. CS 61C L05 C Structs (2)

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 L05 C Structs (3) 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 L05 C Structs (4)

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 L05 C Structs (5) Pointers (4/4) Solution! Pass a pointer to a pointer, 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 L05 C Structs (6)

Dynamic Memory Allocation (1/4) C has operator sizeof() which gives size in bytes (of type or variable) Assume size of objects 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 is not a real function - it does its work at compile time. So: char foo[3*sizeof(int)] is valid, But neither of these is valid in C: char foo[3*myfunction(int)] char foo[3*myfunction(7)] CS 61C L05 C Structs (7) 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. CS 61C L05 C Structs (8)

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. CS 61C L05 C Structs (9) 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), and the usual result is that you corrupt the memory allocator's internal structures -- but you won't find out until much later on, in some totally unrelated part of your code. CS 61C L05 C Structs (10)

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(point p) As always in C, the argument is passed by value - a copy is made. printf( (%d,%d), p.x, p.y); struct point p1 = 0,10; PrintPoint(p1); CS 61C L05 C Structs (11) 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); CS 61C L05 C Structs (12)

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 L05 C Structs (13) 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; ; CS 61C L05 C Structs (14)

Linked List Example /* add a string to an existing list */ char *s1 = abc, *s2 = ; Node *thelist = NULL; CS 61C L05 C Structs (15) thelist = list_add(thelist, s1); thelist = list_add(thelist, s2); Linked List Example /* add a string to an existing list, 2nd call */ node:? list: string: NULL CS 61C L05 C Structs (16)

Linked List Example /* add a string to an existing list */ node:?? list: string: NULL CS 61C L05 C Structs (17) Linked List Example /* add a string to an existing list */ node:????? list: string: NULL CS 61C L05 C Structs (18)

Linked List Example /* add a string to an existing list */ node:? list: string: NULL CS 61C L05 C Structs (19) Linked List Example /* add a string to an existing list */ node: list: string: NULL CS 61C L05 C Structs (20)

Linked List Example /* add a string to an existing list */ node: NULL CS 61C L05 C Structs (21) And in Conclusion Can use pointers 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 L05 C Structs (22)

Question int main(void) int A[] = 5,10; int *p = A; 5 10 A[0] A[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]); *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! p CS 61C L05 C Structs (23)