# ECE 15B COMPUTER ORGANIZATION

Size: px
Start display at page:

Transcription

1 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

2 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

3 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

4 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 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

5 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

6 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

7 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

8 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

9 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

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

CS61C Machine Structures Lecture 4 C Pointers and Arrays 1/25/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Pointers (1) Common C Error There is a difference

### 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 L03 Introduction to C (pt 2) (1)! inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 3 Introduction to C (pt 2) 2010-06-23!!!Instructor Paul Pearce! The typical! development cycle!

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 3 Introduction to C (pt 2) 2010-06-23! C help session: Tonight 7:00-9:00pm @ 306 Soda!!!Instructor Paul Pearce! The typical! development

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 C Strings, Arrays, & Malloc 2007-06-27 Scott Beamer, Instructor Sun announces new supercomputer: Sun Constellation CS61C L3 C Pointers

### More C Pointer Dangers

CS61C L04 Introduction to C (pt 2) (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Must-see talk Thu 4-5pm @ Sibley by Turing Award winner Fran Allen: The Challenge of Multi-Cores: Think Sequential,

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

inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #2 - C Pointers and Arrays Administrivia Buggy Start Lab schedule, lab machines, HW0 due tomorrow in lab 2009-06-24 HW1 due Monday by 9:30am

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

CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture

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

CS61C L3 C Pointers (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 More C intro, C Strings, Arrays, & Malloc Albert Chae Instructor 2008-06-25 Number Review Sign and magnitude

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

Lecture 4: Outline I. Pointers A. Accessing data objects using pointers B. Type casting with pointers C. Difference with Java references D. Pointer pitfalls E. Use case II. Arrays A. Representation in

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 C Pointers 2004-09-08 Lecturer PSOE Dan Garcia www.cs.berkeley.edu/~ddgarcia Cal flies over Air Force We re ranked 13 th in the US and

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

CS61C L4 C Pointers (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 C Strings, Arrays, & Malloc Albert Chae Instructor 2008-06-26 Review: C Strings A string in C is just an array

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

How about them A s!! Go Oaktown!! CS61C - Machine Structures Lecture 4 C Structures Memory Management 2002-09-05 Dan Garcia (www.cs.berkeley.edu/~ddgarcia) Dave Patterson (www.cs.berkeley.edu/~patterson)

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

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

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

CS61C Machine Structures Lecture 4 C Structs & Memory Management 9/5/2007 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Structs (1) C String Standard Functions

### CS61C : Machine Structures

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

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 Introduction to C (pt 2) 2014-09-08!!!Senior Lecturer SOE Dan Garcia!!!www.cs.berkeley.edu/~ddgarcia! C most popular! TIOBE programming

### CS61C : Machine Structures

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

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #4 C Memory Management 2007-06-28 Scott Beamer, Instructor iphone Comes out Tomorrow CS61C L4 C Memory Management (1) www.apple.com/iphone

### 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: Justin Hsia 6/20/2012 Summer 2012 Lecture #3 1 Review of Last Lecture C Basics Variables, Functions, Flow Control,

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

CS61C L04 Introduction to C (pt 2) (1)! Reference slides! C Strings! You ARE responsible for the material on these slides (they re just taken from the reading anyway). These were the slides that generated

### CS 61c: Great Ideas in Computer Architecture

Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address

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

Reference slides! You ARE responsible for the material on these slides (they re just taken from the reading anyway). These were the slides that generated the fewest questions in years past (i.e., those

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 4 Introduction to C (pt 2) C Pointers CS61C L03 Introduction to C (pt 2) (1) 2006-09-06 Lecturer SOE Dan Garcia www.cs.berkeley.edu/~ddgarcia

### Lectures 13 & 14. memory management

Lectures 13 & 14 Linked lists and memory management Courtesy of Prof. Garcia (UCB) CS61C L05 Introduction to C (pt 3) (1) Review Pointers and arrays are virtually same C knows how to increment pointers

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

CS 61C: Great Ideas in Computer Architecture C Pointers Instructors: Vladimir Stojanovic & Nicholas Weaver http://inst.eecs.berkeley.edu/~cs61c/sp16 1 Agenda Pointers Arrays in C 2 Address vs. Value Consider

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

CS61C L3 C Pointers (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 C Pointers 2005-09-07 There is one handout today at the front and back of the room! Lecturer PSOE, new dad Dan

### C Arrays, Strings, More Pointers Instructor: Steven Ho

C Arrays, Strings, More Pointers Instructor: Steven Ho Review of Last Lecture C Basics Variables, Functions, Flow Control, Types, and Structs Only 0 and NULL evaluate to FALSE Pointers hold addresses Address

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

IT 252 Computer Organization and Architecture Introduction to the C Programming Language Chia-Chi Teng Administrivia : You have a question? Tips on getting an answer to your question: Ask a classmate Catch

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

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II Instructor: Randy H. Katz hbp://inst.eecs.berkeley.edu/~cs61c/fa13 9/10/13 Fall 2013 - - Lecture #4 1 Agenda Pointers and Arrays Administrivia

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

CS61C L05 Introduction to C (pt 3) (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 Introduction to C (pt 3) C Memory Management 2008-02-01 Lecturer SOE Dan Garcia Hello to Scott

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management 2010-06-28!!! Instructor Paul Pearce! Symmetric multiprocessor! MIPS support for Android MIPS Technologies (founded

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

Lecture 5: Outline I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Multidimensional arrays: 2D Declaration int a[3][4]; /*Conceptually 2D matrix

### Review! Lecture 5 C Memory Management !

CS61C L05 C Memory Management (1)! inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 5 C Memory Management 2010-06-28!!! Instructor Paul Pearce! Symmetric multiprocessor! MIPS support for

### Arrays and Memory Management

Arrays and Memory Management 1 Pointing to Different Size Objects Modern machines are byte-addressable Hardware s memory composed of 8-bit storage cells, each has a unique address A C pointer is just abstracted

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

CS 61C Faux Midterm Review (1) CS61C Machine Structures Faux Midterm Review 2002-09-29 Jaein Jeong Cheng Tien Ee www-inst.eecs.berkeley.edu/~cs61c/ Numbers: positional notation Number Base B B symbols

### Lecture 2: C Programm

0 3 E CS 1 Lecture 2: C Programm ing C Programming Procedural thought process No built in object abstractions data separate from methods/functions Low memory overhead compared to Java No overhead of classes

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

Agenda CS 61C: Great Ideas in Computer Architecture Introduc;on to C, Part II Instructors: Krste Asanovic Randy H. Katz hep://inst.eecs.berkeley.edu/~cs61c/f12 Review Pointers Administrivia Arrays Technology

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

CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Bernhard Boser & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

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

Computer Science 61C Spring 2018 Wawrzynek and Weaver Pointers, Arrays, Memory: AKA the cause of those F@#)(#@*( Segfaults 1 Agenda Computer Science 61C Spring 2018 Pointers Arrays in C Memory Allocation

Lecture'3'(P a rt1):'''''''' ' 0 3 ' C 'Programm E S C ing'' 1 Functions:"" Breaking"large"computing"tasks"to" smaller"ones" 2 C'Programming' 'Procedural'thought'process' main%()%/*%high%level%outline%*/%

### 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 Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c And in Conclusion, 2 Processor Control Datapath Components of a Computer PC Registers Arithmetic

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

CS 61C: Great Ideas in Computer Architecture Lecture 3: Pointers Krste Asanović & Randy Katz http://inst.eecs.berkeley.edu/~cs61c Agenda Pointers in C Arrays in C This is not on the test Pointer arithmetic

' 0 3 ' CSE 1 Lecture'3:''' ''''''''''''' C'Programm ing'' C'Programming' 'Procedural'thought'process' 'No'built'in'object'abstractions' data'separate'from'methods/functions' Low'memory'overhead'compared'to'Java'

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

Operating Systems 2INC0 C course Pointer Advanced Dr. Ir. Ion Barosan (i.barosan@tue.nl) Containt Pointers Definition and Initilization Ponter Operators Pointer Arithmetic and Array Calling Functions by

### CS24 Week 2 Lecture 1

CS24 Week 2 Lecture 1 Kyle Dewey Overview C Review Void pointers Allocation structs void* (Void Pointers) void* Like any other pointer, it refers to some memory address However, it has no associated type,

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

CSE 30: Computer Organization and Systems Programming Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers Diba Mirza University of California, San Diego 1 Q: Which of the assignment

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

+ More on Pointers + Null pointers In Java we have the keyword null, which is the value of an uninitialized reference type In C we sometimes use NULL, but its just a macro for the integer 0 Pointers are

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

CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II Instructors: Krste Asanovic & Vladimir Stojanovic h>p://inst.eecs.berkeley.edu/~cs61c/sp15 1 Review: Components of a Computer Processor

### CS61C : Machine Structures

inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #6: Memory Management CS 61C L06 Memory Management (1) 2006-07-05 Andy Carle Memory Management (1/2) Variable declaration allocates

### Intermediate Programming, Spring 2017*

600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

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

EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and

### Pointers and Arrays 1

Pointers and Arrays 1 Pointers and Arrays When an array is declared, The compiler allocates sufficient amount of storage to contain all the elements of the array in contiguous memory locations The base

### EM108 Software Development for Engineers

EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and

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

CMSC 313 Introduction to Computer Systems Lecture 8 Pointers, cont. Alan Sussman als@cs.umd.edu Administrivia Project 2 posted, due October 6 public tests s posted Quiz on Wed. in discussion up to pointers

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

Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

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

CS61CL L02 Dynamic Storage (1) inst.eecs.berkeley.edu/~cs61c CS61CL : Machine Structures Lecture #3 - Dynamic Storage 2009-06-29 Jeremy Huddleston Review Pointers and arrays are virtually same C knows

### Character Strings. String-copy Example

Character Strings No operations for string as a unit A string is just an array of char terminated by the null character \0 The null character makes it easy for programs to detect the end char s[] = "0123456789";

### Lecture 10: Program Development versus Execution Environment

Lecture 10: Program Development versus Execution Environment CSE 30: Computer Organization and Systems Programming Winter 2010 Rajesh Gupta / Ryan Kastner Dept. of Computer Science and Engineering University

### Procedural programming with C

Procedural programming with C Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 77 Functions Similarly to its mathematical

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

Review: Components of a Computer CS 61C: Great Ideas in Computer Architecture Introduc)on to C, Part II Processor Control Datapath PC Enable? Read/Write Address Memory Program Bytes Input Instructors:

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

High-performance computing and programming Intro to C on Unix/Linux IT Uppsala universitet What is C? An old imperative language that remains rooted close to the hardware C is relatively small and easy

### Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

### Arrays and Pointers in C. Alan L. Cox

Arrays and Pointers in C Alan L. Cox alc@rice.edu Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including

### CS 11 C track: lecture 5

CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap Pointers (from last week) Address: location where data stored

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

THE GOOD, BAD AND UGLY ABOUT POINTERS Problem Solving with Computers-I The good: Pointers pass data around efficiently Pointers and arrays 100 104 108 112 116 ar 20 30 50 80 90 ar is like a pointer to

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

CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make

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

C++ ARRAYS POINTERS POINTER ARITHMETIC Problem Solving with Computers-I General model of memory Sequence of adjacent cells Each cell has 1-byte stored in it Each cell has an address (memory location) Memory

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013 TOPICS TODAY Reminder: MIDTERM EXAM on THURSDAY Pointer Basics Pointers & Arrays Pointers & Strings Pointers & Structs

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

CS2351 Data Structures Lecture 7: A Brief Review of Pointers in C 1 About this lecture Pointer is a useful object that allows us to access different places in our memory We will review the basic use of

### DAY 3. CS3600, Northeastern University. Alan Mislove

C BOOTCAMP DAY 3 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh and Pascal Meunier s course at Purdue Memory management 2 Memory management Two

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

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements

### Lecture 7: Procedures and Program Execution Preview

Lecture 7: Procedures and Program Execution Preview CSE 30: Computer Organization and Systems Programming Winter 2010 Rajesh Gupta / Ryan Kastner Dept. of Computer Science and Engineering University of

### Lecture 04 Introduction to pointers

Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable

### Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

### Array Initialization

Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]

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

Pointer Basics What is a pointer? pointer = memory address + type C Language III CMSC 313 Sections 01, 02 A pointer can contain the memory address of any variable type A primitive (int, char, float) An

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

Midterm Topic Review Pointer Basics C Language III CMSC 313 Sections 01, 02 1 What is a pointer? Why Pointers? Pointer Caution pointer = memory address + type A pointer can contain the memory address of

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

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Outline Review pointers New: Strings New: Variable Scope (global vs. local variables)

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

Pointers Pointers Every location in memory has a unique number assigned to it called it s address A pointer is a variable that holds a memory address A pointer can be used to store an object or variable

### Low-Level C Programming. Memory map Pointers Arrays Structures

Low-Level C Programming Memory map Pointers Arrays Structures Memory Map 0x7FFF_FFFF Binaries load at 0x20000 by default Stack start set by binary when started Stack grows downwards You will need one stack

### Arrays and Pointers in C & C++

Arrays and Pointers in C & C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++,

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

Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic

### Data Representation and Storage. Some definitions (in C)

Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use pointer arithmetic correctly Explain

### Memory Management in C (Dynamic Strings) Personal Software Engineering

Memory Management in C (Dynamic Strings) Personal Software Engineering Memory Organization Function Call Frames The Stack The call stack grows from the top of memory down. sp Available for allocation The

### C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

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

Chapter 16 Pointers and Arrays Based on slides McGraw-Hill Additional material 200/2005 Lewis/Martin Pointers and Arrays We've seen examples of both of these in our LC- programs; now we'll see them in

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

CS 449 Lecture 3 Dr. Jack Lange int x = 4; Variables in memory X is a handle to 4 bytes of memory assigned by the compiler &x; à returns the memory locaeon What if we want to modify the value of x in another

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

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

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

Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

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

Pointers Page 1 So far, system calls have had easy syntax Wednesday, September 30, 2015 10:45 AM Integer, character string, and structure arguments. But this is not always true. Today, we begin to explore

### CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CSC209H Lecture 4 Dan Zingaro January 28, 2015 Strings (King Ch 13) String literals are enclosed in double quotes A string literal of n characters is represented as a n+1-character char array C adds a

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= &num;

### SYSC 2006 C Winter 2012

SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization

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

CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III Instructors: John Wawrzynek & Vladimir Stojanovic http://inst.eecs.berkeley.edu/~cs61c/fa15 1 Review, Last Lecture Pointers are

### CS 314 Principles of Programming Languages. Lecture 11

CS 314 Principles of Programming Languages Lecture 11 Zheng Zhang Department of Computer Science Rutgers University Wednesday 12 th October, 2016 Zheng Zhang 1 eddy.zhengzhang@cs.rutgers.edu Class Information

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

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 TOPICS TODAY Project 5 Pointer Basics Pointers and Arrays Pointers and Strings POINTER BASICS Java Reference In Java,

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

Announcements Thursday Extras: CS Commons on Thursdays @ 4:00 pm but none next week No office hours next week Monday or Tuesday Reflections: when to use if/switch statements for/while statements Floating-point