CS-240 Data Structures in C Arrays Dick Steflik. Abstract Data Type A collection of pairs where index is an ordered set of integers and are values of.

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

Pointers.
CHP-5 LinkedList.
PZ04B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ04B - Arrays and records Programming Language Design.
Arrays and records Programming Language Design and Implementation
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Chapter 7:: Data Types Programming Language Pragmatics
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Informática II Prof. Dr. Gustavo Patiño MJ
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Arrays in CCS-2301, B-Term Arrays in C (including a brief introduction to pointers) CS-2301, System Programming for Non-Majors (Slides include materials.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Run-time Environment and Program Organization
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
Compiler Construction
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Intro to Data Structures and ADTs Chapter 2. 2 Goal of Data Structures Organize data Facilitate efficient … –storage –retrieval –manipulation Select.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Assembly - Arrays תרגול 7 מערכים.
Data Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
◦ A one-dimensional array in C is declared implicitly by appending brackets to the name of a variable int list[5], *plist[5]; ◦ arrays start at index.
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Windows Programming Lecture 03. Pointers and Arrays.
APS105 Malloc and 2D Arrays (text Ch6.4, Ch10.2).
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 6 – Data Types CSCE 343.
Run-time organization
CSCI206 - Computer Organization & Programming
CSCE 210 Data Structures and Algorithms
Dr Tripty Singh Arrays.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

CS-240 Data Structures in C Arrays Dick Steflik

Abstract Data Type A collection of pairs where index is an ordered set of integers and are values of some data type that is constant for the array. not all languages require index to be continuous or contiguous or start at 0 or 1. In C arrays are zero based and are contiguous from 0 to size-1 and can contain any simple or aggregate data type

ADT (cont.) Pascal allows discontinous indicies –A(2:5, 10:20, 26) other index values are undefined and take up no memory Perl allows indicies that are not integers but are literal values (called an associative array) –A[tom], A[dick], A[harry]

ADT (cont.) Static arrays – arrays allocated at compile time Dynamic arrays – arrays allocated by the storage management system at program run time

ADT Operations Basic operations: create(A) – allocates storage retrieve(A,i) – return v at position i in A store(A,I,v) – store v at position i in A destroy(A) – deallocate storage associated with A

create static storage : int a[10]; //40 bytes char word[25]; //25 bytes –allocated as part of the program space by the compiler. –&a is equivalent to a and is the address of a[0] –once allocated cannot be deallocated, will always take up program space can be initialized by compiler using an initializer (ex. int A[5] = (0,0,0,0,0); )

create Dynamic : storage is allocated at run-time using malloc, cmalloc or realloc #define SIZE 10 int * myarray; myarray = (int *) malloc (SIZE*sizeof(int)); makes an array of 10 integers names myarray cmalloc works same way but initializes array to 0 initialization to anything else requires a loop realloc will resize a previously allocated array to bigger or smaller since this happens at run time, time is expended

store done the same way for both static and dynamic arrays by using the assignment operator (=) a[5] = 9;

retrieve retrieving a value from some position in an array is done the same way for both static and dynamic arrays using the array position implicitly. x[3] ; //the value of the 4 th element of x can be used this way in any assignment, arithmetic/logical operation or as an argument in a function call

destroy destruction of a statically allocated array happened when the program is done destruction of dynamically allocated arrays is done using the free(arrayname) function, this returns the storage to the storage management system for subsequent allocation for something else. forgetting to deallocate unneeded storage is called a “memory leak” and can cause a program terminate abnormally (crash)

memory remember, a computer’s memory is really an array of bytes (indicies 0 to size-1) every time an array access (retrieve or store) is done the machine must make a calculation to see where in memory the desired location is: ex int a[5]; a[3]=2; to calculate the address of a[3] address=base address+(index*element size) = (3 16 *4 16 ) = 100c 16 base address is assigned by compiler for static and by SMS for dynamic and kept track of in a system table for run time

Structures Allows us to create and aggregate data type: typedef struct person { char name[10]; int age; } person tom; - tom takes up 14 bytes of storage; 10 for name and the next 4 for age

Structures Structures can be embedded within one another: typedef struct date { int month; int date; int year; }; typedef struct student { char name[16]; date dateOfBirth; }; date pearlHarborDay; // 12 bytes of storage student typical; // 18 bytes of storage student class[30]; // 540 bytes of storage

Unions A union is like a structure but the fields don’t always have to have the same definition typedef struct sextype { enum tag (female, male) sex; union { int children; char beard; } u; }; typedef struct human { char name[10]; short age; float salary; date dob; sextype sexinfo; }; The compiler will always reserve the maximum number bytes for the union; i.e. even though sextype for wormen is 4 bytes and only one byte for men the compiler will always reserve 4.

Self-Referential Structures Structure that refers to an item of the same type. used for dynamic data structures like lists and trees. typedef struct node { int key; node * next; }

Array Mapping Functions Used by the compiler to help calculate the effective address of an array element in memory Takes into account: base address the dimension the element size

2 dimensional arrays int a[2][2] can be visualized as a 2x2 square matrix but is really an array of two elements where each element is an array of two ints

cont. 0x x x x10000C 0,0 0,1 1,0 1,1 int a[2][2] This storage arrangement is known as: Row Major Order SMF = base addr + (dim(n) * element size * index m ) + (element size * index n ) a[m][n] ) ex. a[1][1] addr = (2 * 4 * 1) + (4 * 1) = = 0x10000C

Sparse Arrays arrays where many or most of the elements will have the value zero (or possibly the same value) examples: high order polynomials, bit mapped graphics, linear algebra ( diagional matricies(identity matrix, tridiagonal, banded), triangular matrices, )

Polynomial representation // an array of struct #define MAXTERMS 10 typedef struct term { real coeff; int expnt;} term poly1[MAXSIZE]; term poly2[MAXSIZE]; term poly3[MAXSIZE]; one dimensional array where: index represents the exponent and the stored value is the corresponding coefficient x 8 + 4x OR

Identity Matrix Only has values on the major diagonal v 00 v00 00v AMF[m][n] if (m == n) return v else return 0 map it on top of a one dimensional array of three elements v v vAMF[m][n] if ( m == n ) return A[m] else return 0