C Tutorial (part 5) CS220, Spring 2012

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
University of Washington Data Structures! Arrays  One-dimensional  Multi-dimensional (nested)  Multi-level Structs  Alignment Unions 1.
Machine-Level Programming IV: Structured Data Apr. 23, 2008 Topics Arrays Structures Unions EECS213.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
Structured Data I: Homogenous Data Sept. 17, 1998 Topics Arrays –Single –Nested Pointers –Multilevel Arrays Optimized Array Code class08.ppt “The.
Data Representation and Alignment Topics Simple static allocation and alignment of basic types and data structures Policies Mechanisms.
ECE 454 Computer Systems Programming Memory performance (Part II: Optimizing for caches) Ding Yuan ECE Dept., University of Toronto
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
University of Amsterdam Computer Systems – Data in C Arnoud Visser 1 Computer Systems New to C?
University of Washington Today Lab 3 out  Buffer overflow! Finish-up data structures 1.
Carnegie Mellon 1 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Machine-Level Programming IV: Data Topics: Arrays.
Fabián E. Bustamante, Spring 2007 Machine-Level Prog. IV - Structured Data Today Arrays Structures Unions Next time Arrays.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Assembly - Arrays תרגול 7 מערכים.
C arrays (Reek, Ch. 8) CS 3090: Safety Critical Programming in C.
ECE Application Programming
Chapter 6 – Data Types CSCE 343.
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data
Arrays CSE 351 Spring 2017 Instructor: Ruth Anderson
Machine-Level Programming IV: Structured Data
Numeric Arrays Numeric Arrays Chapter 4.
Machine-Level Programming IV: Data
Lecture 7 – Arrays (1) PGT 106 : C PROGRAMMING.
Arrays.
Machine-Level Programming IV: Structured Data Sept. 30, 2008
Heterogeneous Data Structures & Alignment
Referencing Examples Code Does Not Do Any Bounds Checking!
Machine-Level Programming 5 Structured Data
CSC 253 Lecture 8.
Machine-Level Programming IV: Data CSCE 312
Machine-level Programming IV: Data Structures
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Arrays CSE 351 Winter
Feb 2 Announcements Arrays/Structs in C Lab 2? HW2 released
Machine-Level Programming 6 Structured Data
Machine-Level Programming IV: Structured Data Sept. 19, 2002
Machine-Level Programming IV: Data
CSC 253 Lecture 8.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
نوع داده هاي انتزاعي Abstract Data Types
Instructor: Phil Gibbons
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Instructors: Majd Sakr and Khaled Harras
EKT150 : Computer Programming
Machine-Level Programming IV: Data
Machine-Level Programming IV: Data /18-213/14-513/15-513: Introduction to Computer Systems 8th Lecture, September 20, 2018.
Instructors: Majd Sakr and Khaled Harras
Multidimensional Arrays
Machine Level Representation of Programs (III)
Machine-Level Programming IV: Machine Data 9th Lecture
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Arrays CSE 351 Winter 2018 Instructor: Mark Wyse Teaching Assistants:
Machine-Level Programming 5 Structured Data
Multidimensional array
Executables & Arrays CSE 351 Autumn 2018
Machine-Level Programming 5 Structured Data
Dr Tripty Singh Arrays.
INC 161 , CPE 100 Computer Programming
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Structured Data I: Homogenous Data Feb. 10, 2000
Instructor: Fatma CORUT ERGİN
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
Data Spring, 2019 Euiseong Seo
Machine-Level Programming VIII: Data Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017 Systems book chapter 3* * Modified slides.
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
C# Language & .NET Platform 10th Lecture
Machine-Level Programming IV: Data
Presentation transcript:

C Tutorial (part 5) CS220, Spring 2012 Slides based on 213 slides. Modified example slightly and removed all assembly and assembly related content.

Array Allocation Basic Principle T A[L]; Array of data type T and length L Contiguously allocated region of L * sizeof(T) bytes char string[12]; x x + 12 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 double a[3]; x + 24 x x + 8 x + 16 char *p[3]; x x + 4 x + 8 x + 12 IA32 x x + 8 x + 16 x + 24 x86-64

Array Access Basic Principle Reference Type Value Will disappear T A[L]; Array of data type T and length L Identifier A can be used as a pointer to array element 0: Type T* Reference Type Value val[4] int 3 val int * x val+1 int * x + 4 &val[2] int * x + 8 val[5] int ?? *(val+1) int 5 val + i int * x + 4 i int val[5]; 1 5 2 3 x x + 4 x + 8 x + 12 x + 16 x + 20 Will disappear Blackboard?

Array Access Basic Principle Reference Type Value T A[L]; Array of data type T and length L Identifier A can be used as a pointer to array element 0: Type T* Reference Type Value val[4] int 3 val int * x val+1 int * x + 4 &val[2] int * x + 8 val[5] int ?? *(val+1) int 5 val + i int * x + 4 i int val[5]; 1 5 2 3 x x + 4 x + 8 x + 12 x + 16 x + 20

Array Example Int cs[5] = { 1, 5, 2, 1, 3 }; Int ece[5] = { 0, 2, 1, 3, 9 }; Int me[5] = { 9, 4, 7, 2, 0 }; cs: 1 5 2 3 16 20 24 28 32 36 ece: 2 1 3 9 36 40 44 48 52 56 me: 9 4 7 2 56 60 64 68 72 76 Example arrays were allocated in successive 20 byte blocks Not guaranteed to happen in general

Referencing Examples Reference Address Value Guaranteed? cs; 1 5 2 3 16 20 24 28 32 36 ece; 2 1 3 9 36 40 44 48 52 56 me; 9 4 7 2 56 60 64 68 72 76 Reference Address Value Guaranteed? ece[3] 36 + 4* 3 = 48 3 ece[5] 36 + 4* 5 = 56 9 me[-1] 36 + 4*-1 = 32 3 cs[15] 16 + 4*15 = 76 ?? Will disappear Blackboard?

Referencing Examples Reference Address Value Guaranteed? Yes No cmuq; 1 5 2 3 16 20 24 28 32 36 tamuq; 2 1 3 9 36 40 44 48 52 56 nwq; 9 4 7 2 56 60 64 68 72 76 Reference Address Value Guaranteed? ece[3] 36 + 4* 3 = 48 3 ece[5] 36 + 4* 5 = 56 9 ece[-1] 36 + 4*-1 = 32 3 cs[15] 16 + 4*15 = 76 ?? No bound checking Out of range behavior implementation-dependent No guaranteed relative allocation of different arrays Yes No No No

Nested Array Example “Row-Major” ordering of all elements guaranteed int ec[4][5] = {{1, 5, 2, 0, 6}, {1, 5, 2, 1, 3 }, {1, 5, 2, 1, 7 }, {1, 5, 2, 2, 1 }}; 1 5 2 6 1 5 2 3 1 5 2 7 1 5 2 ec 76 96 116 136 156 “Row-Major” ordering of all elements guaranteed What data type is ec? What is its value? What data type is ec[2]? What is its value? What data type is ec[1][1]? What is its value?

Multidimensional (Nested) Arrays Declaration T A[R][C]; 2D array of data type T R rows, C columns Type T element requires K bytes Array Size R * C * K bytes Arrangement Row-Major Ordering A[0][0] A[0][C-1] A[R-1][0] • • • A[R-1][C-1] • int A[R][C]; • • • A [0] [C-1] [1] [R-1] •  •  • 4*R*C Bytes

Nested Array Row Access Row Vectors A[i] is array of C elements Each element of type T requires K bytes Starting address A + i * (C * K) int A[R][C]; • • • A [0] [C-1] A[0] • • • A [i] [0] [C-1] A[i] • • • A [R-1] [0] [C-1] A[R-1] •  •  • •  •  • A A+i*C*4 A+(R-1)*C*4

Strange Referencing Examples 1 5 2 6 1 5 2 3 1 5 2 7 1 5 2 76 96 116 136 156 Reference Address Value Guaranteed? ec[3][3] 76+20*3+4*3 = 148 2 ec[2][5] 76+20*2+4*5 = 136 1 ec[2][-1] 76+20*2+4*-1 = 112 3 ec[4][-1] 76+20*4+4*-1 = 152 1 ec[0][19] 76+20*0+4*19 = 152 1 ec[0][-1] 76+20*0+4*-1 = 72 ?? Will disappear

Strange Referencing Examples 1 5 2 6 1 5 2 3 1 5 2 7 1 5 2 ec[4][5]; 76 96 116 136 156 Reference Address Value Guaranteed? ec[3][3] 76+20*3+4*3 = 148 2 ec[2][5] 76+20*2+4*5 = 136 1 ec[2][-1] 76+20*2+4*-1 = 112 3 ec[4][-1] 76+20*4+4*-1 = 152 1 ec[0][19] 76+20*0+4*19 = 152 1 ec[0][-1] 76+20*0+4*-1 = 72 ?? Code does not do any bounds checking Ordering of elements within array guaranteed Yes Yes Yes Yes No

Multi-Level Array Example Variable univ denotes array of 3 elements Each element is a pointer 4 bytes Each pointer points to array of int’s int cs = { 1, 5, 2, 1, 3 }; int ece = { 0, 2, 1, 3, 9 }; int me = { 9, 4, 7, 2, 0 }; #define DCOUNT 3 int *ec[DCOUNT] = {ece, cs, me}; cs 1 5 2 3 16 20 24 28 32 36 36 160 16 56 164 168 ec ece 2 1 3 9 36 40 44 48 52 56 me 9 4 7 2 56 60 64 68 72 76

Array Element Accesses Nested array cs Multi-level array 1 5 2 3 16 20 24 28 32 36 36 160 16 56 164 168 ec ece 2 1 3 9 36 40 44 48 52 56 me 9 4 7 2 56 60 64 68 72 76 ec[index][dig] looks similar, but element: Mem[ec+20*index+4*dig] Mem[Mem[ec+4*index]+4*dig]

Strange Referencing Examples cs 1 5 2 3 16 20 24 28 32 36 36 160 16 56 164 168 ec ece 2 1 3 9 36 40 44 48 52 56 me 9 4 7 2 56 60 64 68 72 76 Reference Address Value Guaranteed? ec[2][3] 56+4*3 = 68 2 ec[1][5] 16+4*5 = 36 0 ec[2][-1] 56+4*-1 = 52 9 ec[3][-1] ?? ?? ec[1][12] 16+4*12 = 64 7 Will disappear

Strange Referencing Examples cmuq 1 5 2 3 16 20 24 28 32 36 36 160 16 56 164 168 ec tamuq 2 1 3 9 36 40 44 48 52 56 nwq 9 4 7 2 56 60 64 68 72 76 Reference Address Value Guaranteed? ec[2][3] 56+4*3 = 68 2 ec[1][5] 16+4*5 = 36 0 ec[2][-1] 56+4*-1 = 52 9 ec[3][-1] ?? ?? ec[1][12] 16+4*12 = 64 7 Code does not do any bounds checking Ordering of elements in different arrays not guaranteed Yes No No No No