ECE 15B COMPUTER ORGANIZATION

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

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

CS61C : Machine Structures

CS61C : Machine Structures

More C Pointer Dangers

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

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

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

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

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.

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

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

CS61C : Machine Structures

CS61C : Machine Structures

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

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

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

CS61C : Machine Structures

Lectures 13 & 14. memory management

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

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

C Arrays, Strings, More Pointers Instructor: Steven Ho

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

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

CS61C : Machine Structures

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

Review! Lecture 5 C Memory Management !

Arrays and Memory Management

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

Lecture 2: C Programm

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

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

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

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

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

C'Programming' data'separate'from'methods/functions' Low'memory'overhead'compared'to'Java'

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

CS24 Week 2 Lecture 1

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

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

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

CS61C : Machine Structures

Intermediate Programming, Spring 2017*

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

Pointers and Arrays 1

EM108 Software Development for Engineers

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

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

Character Strings. String-copy Example

Lecture 10: Program Development versus Execution Environment

Procedural programming with C

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

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Class Information ANNOUCEMENTS

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Arrays and Pointers in C. Alan L. Cox

CS 11 C track: lecture 5

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

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

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

DAY 3. CS3600, Northeastern University. Alan Mislove

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

Lecture 7: Procedures and Program Execution Preview

Lecture 04 Introduction to pointers

Homework #3 CS2255 Fall 2012

Array Initialization

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Pointers. Memory. void foo() { }//return

Low-Level C Programming. Memory map Pointers Arrays Structures

Arrays and Pointers in C & C++

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

Data Representation and Storage. Some definitions (in C)

Memory Management in C (Dynamic Strings) Personal Software Engineering

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

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

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

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

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

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

SYSC 2006 C Winter 2012

CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III

CS 314 Principles of Programming Languages. Lecture 11

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Memory, Arrays & Pointers

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Transcription:

ECE 15B COMPUTER ORGANIZATION Lecture 13 Strings, Lists & Stacks Announcements HW #3 Due next Friday, May 15 at 5:00 PM in HFH Project #2 Due May 29 at 5:00 PM Project #3 Assigned next Thursday, May 19 Due June 5 at 5:00 PM Quiz #2 In class, next Thursday, May 21 Covers up until todayʼs lecture (#13) Dr. Rahul Singh Review: More C Pointer Dangers Declaring a pointer just allocates space to hold the pointer it does not allocate something to be pointed to! Local variables in C are not initialized they may contain anything. What does the following code do void f() int* x; *x = 5; Review: Pointers & Allocation After declaring a pointer: int *ptr; ptr doesnʼt actually point to anything yet. We can either: make it point to something that already exists, or allocate room in memory for something new that it will point to (this lecture) 1

Review: Pointers & Allocation Pointing to something that already exists: int *ptr, var1, var2; var1 = 5; ptr = &var1; var2 = *ptr; var1 and var2 have room implicitly allocated for them. Declaration int ar[2]; Review: Arrays declares a 2-element integer array int ar[] = 795, 635; declares and fills a 2-element integer array. Accessing elements ar[num]; returns the num th element Review: Arrays Review: Arrays Arrays are (almost) identical to pointers char *string and char string[] Nearly identical declarations They differ in very subtle ways: incrementing, declaration of filled arrays Key Concept: An array variable is a pointer to the first element. Consequences: ar is a pointer ar[0] is the same as *ar ar[2] is the same as *(ar+2) We can use pointer arithmetic to access arrays more conveniently. Declared arrays are only allocated while the scope is valid char *foo() char string[32];...; return string; above is incorrect 2

Arrays Array size n; want to access from 0 to n-1, but test for exit by comparing to address one element past the array int a[10], *p, *q, sum = 0;... p = &a[0]; q = &a[10]; while (p!= q) /* sum = sum + p*; p = p + 1; */ sum += *p++; Is this legal C defines that one element past end of array must be a valid address i.e., not cause a bus error or an address error Arrays Array size n; want to access from 0 to n-1, so you should use counter AND utilize a constant for declaration & incr Wrong int i, a[10]; for(i = 0; i < 10; i++)... Right #define ARRAY_SIZE 10 int i, a[array_size]; for(i = 0; i < ARRAY_SIZE; i++)... Why SINGLE SOURCE OF TRUTH Youʼre utilizing indirection avoiding maintaining two copies of the number 10 Arrays Pitfall: An array in C does not know its own length, and bounds are not checked! Consequence: We can accidentally access off the end of an array. We must pass the array and its size to a procedure which is going to traverse it. Segmentation faults and bus errors: These are VERY difficult to find, so be careful. Pointers to Pointers 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); A q 50 60 70 3

Pointers to Pointers Solution! Pass a pointer to a pointer This is 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 = 60 A q q 50 60 70 C Strings A string in C is just an array of characters. char string[] = ; How do you tell how long a string is Last character is followed by a 0 byte (null terminator) int strlen(char s[]) int n = 0; while (s[n]!= 0) n++; return n; C Strings Headaches One common mistake is to forget to allocate an extra byte for the null terminator. More generally, C requires the programmer to manage memory manually (unlike Java or C++). When creating a long string by concatenating several smaller strings, the programmer must insure there is enough space to store the full string! What if you donʼt know ahead of time how big your string will be Copying strings Why not say: void copy (char sto[ ], char sfrom[ ]) sto = sfrom; We need to make sure that space has been allocated for the destination string Similarly, you probably donʼt want to compare two strings using == 4

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 caller must ensure that dst has enough memory to hold the data to be copied. Defined in the header file string.h 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); 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); How big are structs Recall C operator sizeof() 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 5

Linked List Example Linked List Example (1 of 6) Letʼs look at an example of using: Structures Pointers malloc() 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 ; Linked List Example (2 of 6) Linked List Example (3 of 6) 6

Linked List Example (4 of 6) Linked List Example (5 of 6) Linked List Example (6 of 6) Where allocated int myglobal; main() int temp; Structure declaration does not allocate memory Variable declaration does allocate memory If declare outside a procedure allocated in static storage If declare inside procedure allocated on the stack & freed when procedure returns 7

The Stack Stack Stack includes: Return address Parameters Space for other local variables Stack s contiguous blocks of memory stack pointer tells where top stack is When procedure ends stack is tossed off the stack frees memory for future stack s SP Last In, First Out (LIFO) memory usage main () Stack Pointer a(0); void a (int m) Stack Pointer b(1); void b (int n) Stack Pointer c(2); void c (int o) Stack Pointer d(3); void d (int p) Stack Pointer e(4); stack Who cares about stack management Pointers in C allow access to deallocated memory leading to hard-to-find bugs! int * ptr () int y; y = 3; return &y; ; main () ; SP SP int *stackaddr, content; stackaddr = ptr(); content = *stackaddr; printf("%d", content); /* 3 */ content = *stackaddr; printf("%d", content); /*31514 */ Main ptr() (y==3) SP Main Main printf() (y==) C Memory Management C has 3 pools of memory Static storage global variable storage, basically permanent, entire program run The Stack local variable storage, parameters, return address ("stack " in C) The Heap (dynamic storage) data lives until deallocated by programmer C requires knowing where objects are in memory, otherwise won't work as expect 8

Conclusion Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection Array bounds not checked Variables not automatically initialized (Beware) Cost of efficiency is more overhead for the programmer. C gives you a lot of extra rope but be careful not to hang yourself with it! 9