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

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

CS61C : Machine Structures

CS61C : Machine Structures

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

ECE 15B COMPUTER ORGANIZATION

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

Intro to C: Pointers and Arrays

Lectures 13 & 14. memory management

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

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

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

Character Strings. String-copy Example

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

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

Data Representation and Storage. Some definitions (in C)

However, in C we can group related variables together into something called a struct.

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

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

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

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

Principles of C and Memory Management

Arrays and Strings. Arash Rafiey. September 12, 2017

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

CS 61c: Great Ideas in Computer Architecture

CS61C : Machine Structures

CS24 Week 2 Lecture 1

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Structs and Alignment CSE 351 Spring

CS61C : Machine Structures

Introduction to Scientific Computing and Problem Solving

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

CS 261 Data Structures. Introduction to C Programming

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

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

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

EM108 Software Development for Engineers

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Dynamic Data Structures. CSCI 112: Programming in C

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

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

CS429: Computer Organization and Architecture

Introduction to C: Pointers

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

CS240: Programming in C

CS61C : Machine Structures

Pointers and Arrays 1

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

Data Representation and Storage

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

Unit IV & V Previous Papers 1 mark Answers

CS61C : Machine Structures

Intermediate Programming, Spring 2017*

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

More C Pointer Dangers

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

Memory Management. CSC215 Lecture

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Cpt S 122 Data Structures. Data Structures

6.S096: Introduction to C/C++

C Tutorial Pointers, Dynamic Memory allocation, Makefile

Arrays and Pointers (part 2) Be extra careful with pointers!

ESC101N: Fundamentals of Computing End-sem st semester

speller.c dictionary contains valid words, one per line 1. calls load on the dictionary file


Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

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

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Section Notes - Week 1 (9/17)

Lesson 7. Reading and Writing a.k.a. Input and Output

Chapter 19 Data Structures

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

Chapter 19 Data Structures -struct -dynamic memory allocation

Lectures 5-6: Introduction to C

Midterm Sample Questions. a) What evaluates to FALSE in C? What evaluates to TRUE?

Fundamentals of Programming. Lecture 12: C Structures, Unions, Bit Manipulations and Enumerations

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

COMPSCI 210 Part II Data Structure

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

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

Two Dimensional Arrays

Review! Lecture 5 C Memory Management !

CS61C Midterm Review on C & Memory Management

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

CS 11 C track: lecture 5

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

pointers + memory double x; string a; int x; main overhead int y; main overhead

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

Programming for Electrical and Computer Engineers. Pointers and Arrays

POINTER & REFERENCE VARIABLES

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Contents of Lecture 3

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

BBM 201 DATA STRUCTURES

Transcription:

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 with 3 rows 4 columns */ Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3 a[0][0] a[0][1] a[0][2] a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] Element in row i, column j is retrieved as a[i][j] a is a pointer to an integer array of size 4

Representation in memory int arr[3][4]; /*Conceptually 2D matrix with 3 rows 4 columns */ In memory the elements of arr are stored in a contiguous memory block arr [0,0] [0,1] [0,2] [0,3] [1,0] [1,1] [1,2] [1,3] [2,0] [2,1] [2,2] [2,3] Row 1 elements Row 2 elements Row 3 elements

(arr+i) increments the address of arr by how many bytes? arr [0,0] [0,1] [0,2] [0,3] [1,0] [1,1] [1,2] [1,3] [2,0] [2,1] [2,2] [2,3] A. i*sizeof(int) B. i*sizeof(int*) C. i*sizeof(int)*number of columns

(arr+i) increments the address of arr by how many bytes? arr [0,0] [0,1] [0,2] [0,3] [1,0] [1,1] [1,2] [1,3] [2,0] [2,1] [2,2] [2,3] A. i*sizeof(int) B. i*sizeof(int*) C. i*sizeof(int)*number of columns

arr Express arr[i][j] using arr as a pointer [0,0] [0,1] [0,2] [0,3] [1,0] [1,1] [1,2] [1,3] [2,0] [2,1] [2,2] [2,3] A. *((arr+i) +j) B. *(*(arr+i)+j) C. *((int*)(arr+i)+j) D. *(arr+4*i*sizeof(int)+j*sizeof(int))

arr Express arr[i][j] using arr as a pointer [0,0] [0,1] [0,2] [0,3] [1,0] [1,1] [1,2] [1,3] [2,0] [2,1] [2,2] [2,3] A. *((arr+i) +j) B. *(*(arr+i)+j) : This is what is generally used C. *((int*)(arr+i)+j) D. *(arr+4*i*sizeof(int)+j*sizeof(int))

Declaration char name_1[]= John ; char name_2[]= Paul ; char name_3[]= Rose ; Multi-level arrays char * names[]={name_1, name_2, name_3}; What does the following statement print? printf("\n Lettter: %c\n", names[1][2]);

Multi-level arrays: Representation in memory char name_1[]= John ; char name_2[]= Paul ; char name_3[]= Rose ; char * names[]={name_1, name_2, name_3}; names 0x80 0x60 0x90 0x60 0x80 0x90 P a u l \0 J o h n \0 R o s e \0

Multi-level vs multi-dimensional arrays 1. What does names[1][2] give in each case? 2. Which one needs more memory accesses? 3. When would we prefer multi- level arrays? names 0x80 0x60 0x90 0x60 0x80 0x90 P a u l \0 J o h n \0 R o s e \0 names J o h n \0 P a u l \0 R o s e \0

Why use pointers? Pointers in C If we want to pass a huge struct or array, it s easier to pass a pointer than the whole thing. In general, pointers allow cleaner, more compact code. So what are the drawbacks? Pointers are probably the single largest source of bugs in software, so be careful anytime you deal with them.

Derived data types: Structures

C structures : Overview A struct is a data structure composed of 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); }

Pointers to structures 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);

Representation in memory struct p { int y; char x; }; struct p sp; sp y (4 bytes) x (1byte) 0x100 0x104 0x105

struct p { char x; int y; }; Struct p sp; Data Alignment Q: How is the above struct laid out in memory?

Alignment Fundamentals Processors do not always access memory in byte sized chunks, instead in 2, 4, 8, even 16 or 32 byte chunks Boundaries at which data objects are stored affects the behavior of read/write operations into memory 0x00 0x01 0x02 0x03 0x04 0x05 Programmer's view of memory 0x00 0x02 0x04 Processor s view of memory

Alignment Fundamentals Consider the task of reading 4 bytes from memory starting at 0x00 for processors that read 1, 2, 4 bytes at a time. How many memory accesses are needed in each case? 0x00 0x01 0x02 0x03 0x04 0x05 0x00 0x01 0x02 0x03 0x04 0x05 0x00 0x02 0x04 0x00 0x04 Memory 1 byte reads 2 byte reads 4 byte reads

Alignment Fundamentals Now consider the task of reading 4 bytes from memory starting at 0x01 for processors that read 1, 2, 4 bytes at a time. How many memory accesses are needed in each case? 0x00 0x01 0x02 0x03 0x04 0x05 Some processors just would not allow this scenario because it is extra work for the h/w and it affects the performance 0x00 0x01 0x02 0x03 0x04 0x05 0x00 0x02 0x04 0x00 0x04 Memory 1 byte reads 2 byte reads 4 byte reads Additional operations needed to get required bytes

Alignment requirements Data objects should start at memory addresses that are divisible by the size of the data object. short (2 byte) at address divisible by 2 0b _ 0 int (4 byte) at address divisible by 4 0b 00 double (8 byte) at address divisible by 8 0b 000 struct p { char x; int y; }; Struct p sp; Q: How is sp laid out in memory? Compiler takes care of zero padding sp x 0 0 0 y 0x100 0x104 0x108

How big are structs? Recall C operator sizeof() which gives size in bytes (of type or variable) Q: How big is sizeof(struct p)? struct p { char x; int y; }; A. 5 bytes B. 8 bytes C. 12 bytes

Linked Lists Node 1 Node 2 Node 3 Data A generic linked list has: Multiple data objects (structs) also called nodes Each node linked to the next node in the list i.e. it knows the address of the next node Nodes located at different memory locations (unlike arrays where they are contiguous) Advantages compared to arrays Multiple data members (of different data types) can be stored in each node Nodes can be easily inserted or removed from the list without modifying the whole list

Let s look at an example of using structures, pointers, malloc(), and free() to implement a linked list of strings. typedef struct Node node; struct Node { char *value; next; }; node value Q:What is the data type of the variable next? A. struct Node B. Node C. node D. node * next

Let s look at an example of using structures, pointers, malloc(), and free() to implement a linked list of strings. typedef struct Node node; struct Node { char *value; next; }; node value Q:What is the data type of the variable next? A. struct Node B. Node C. node D. node * next

Adding a node to the list node *list_add(node* head, char *string) { //Step 1:Create a new node ; return new_node; value=? } new_node Q: How should we declare and initialize new_node? next=? A. node *new_node=(node*) malloc(sizeof(node)); B. node new_node; C. node *new_node=head; D. node *new_node=(node *)malloc(sizeof(head));

Adding a node to the list node *list_add(node* head, char *string) { //Step 1:Create a new node ; return new_node; value=? } new_node Q: How should we declare and initialize new_node? next=? A. node *new_node=(node*) malloc(sizeof(node)); B. node new_node; C. node *new_node=head; D. node *new_node=(node *)malloc(sizeof(head));

Q:What is wrong with step 2? node *list_add(node* head, char *string) { //Step 1: Create a new node node *new_node=(node*) malloc(sizeof(node)); //Step 2: Fill in its value strcpy(new_node->value, string); return new_node; } A. Step 2 is correct. B. We should use the operator. instead of -> C. Memory is not allocated for value

Q:What is wrong with step 2? node *list_add(node* head, char *string) { //Step 1: Create a new node node *new_node=(node*) malloc(sizeof(node)); //Step 2: Fill in its value strcpy(new_node->value, string); return new_node; } A. Step 2 is correct. B. We should use the operator. instead of -> C. Memory is not allocated for value

So far. node *list_add(node* head, char *string) { //Step 1: Create a new node node *new_node=(node*) malloc(sizeof(node)); //Step 2: Fill in its value new_node->value = (char*) malloc(strlen(string)+1); strcpy(new_node->value, string); return new_node; } new_node head" string" abc value abc next=? NULL

What should Step 3 be? node *list_add(node* head, char *string){ //Step 1: Create a new node node *new_node=(node*) malloc(sizeof(node)); //Step 2: Fill in its value new_node->value = (char*) malloc(strlen(string)+1); strcpy(new_node->value, string); //Step 3: return new_node; } new_node head" A. new_node->next =head; B. next=head; C. head=new_node; D. new_node->next =*head; value abc next=? NULL

What should Step 3 be? node *list_add(node* head, char *string){ //Step 1: Create a new node node *new_node=(node*) malloc(sizeof(node)); //Step 2: Fill in its value new_node->value = (char*) malloc(strlen(string)+1); strcpy(new_node->value, string); //Step 3: Link new_node to the head of the list new_node->next =head; return new_node; } new_node head" A. new_node->next =head; B. next=head; C. head=new_node; D. new_node->next =*head; value abc next NULL