Complex data structures. Cedric Saule

Similar documents
Structures and Pointers

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

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

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

Pointers. Cedric Saule

CS349/SE382 A1 C Programming Tutorial

Dynamic Memory Allocation and Command-line Arguments

Arrays and Pointers (part 1)

Arrays and Pointers (part 1)

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

typedef int Array[10]; String name; Array ages;

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

High Performance Computing MPI and C-Language Seminars 2009

Dynamic Memory Allocation

Functions. Cedric Saule

Arrays in C. Data and File Structures Laboratory. DFS Lab (ISI) Arrays in C 1 / 1

Dynamic Data Structures. CSCI 112: Programming in C

Institute For Arts & Digital Sciences SHORT COURSES

C-types: basic & constructed. C basic types: int, char, float, C constructed types: pointer, array, struct

Kurt Schmidt. October 30, 2018

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

Arrays. What if you have a 1000 line file? Arrays

CS 161 Exam II Winter 2018 FORM 1

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

Pointers (continued), arrays and strings

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Note: The enumerations range from 0 to (number_of_elements_in_enumeration 1).

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

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

Calendar Excel Template User Guide

Exercises Software Development I. 02 Algorithm Testing & Language Description Manual inspection, test plan, grammar, metasyntax notations (BNF, EBNF)

Object-Oriented Principles and Practice / C++

Arrays. C Types. Derived. Function Array Pointer Structure Union Enumerated. EE 1910 Winter 2017/18

Scheduling. Scheduling Tasks At Creation Time CHAPTER

CSE 305 Programming Languages Spring, 2010 Homework 5 Maximum Points: 24 Due 10:30 AM, Friday, February 26, 2010

Principles of C and Memory Management

Introduction to C. Sami Ilvonen Petri Nikunen. Oct 6 8, CSC IT Center for Science Ltd, Espoo. int **b1, **b2;

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Pointers (continued), arrays and strings

C Programming Basics II

DCS/100 Procedural Programming

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Pointers and Terminal Control

High Performance Computing

Arrays. Arrays (8.1) Arrays. One variable that can store a group of values of the same type. Storing a number of related values.

22c:111 Programming Language Concepts. Fall Types I

A Beginner s Guide to Programming Logic, Introductory. Chapter 6 Arrays

Memory Allocation. General Questions

Objectives. Describe ways to create constants const readonly enum

[0569] p 0318 garbage

Arrays and Memory Management

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

ControlLogix/Studio 5000 Logix Designer Course Code Course Title Duration CCP143 Studio 5000 Logix Designer Level 3: Project Development 4.

Incoming Exam. CS 201 Introduction to Pointers. What is a Pointer? Pointers and Addresses. High Speed Memory (RAM) Size of Variable Types.

Why arrays? To group distinct variables of the same type under a single name.

Arrays, Pointers and Memory Management

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

1 of 8 10/10/2018, 12:52 PM RM-01, 10/10/2018. * Required. 1. Agency Name: * 2. Fiscal year reported: * 3. Date: *

CS201- Introduction to Programming Current Quizzes

INFORMATION TECHNOLOGY SPREADSHEETS. Part 1

Fundamental of Programming (C)

Chapter 7. Pointers. Copyright 2007 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CHAPTER 5 FLOW OF CONTROL

CSC2/454 Programming Languages Design and Implementation Records and Arrays

CSCI312 Principles of Programming Languages!

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Lecture 3. The syntax for accessing a struct member is

A Fast Review of C Essentials Part I

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

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

Laboratory 10 POINTERS I. FUNDAMENTALS

Dynamic memory allocation

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Chapter 4: Arrays, Pointers and Strings

just a ((somewhat) safer) dialect.

Cambridge English Dates and Fees for 2018

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

AIMMS Function Reference - Date Time Related Identifiers

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

TYPES, VALUES AND DECLARATIONS

Chapter 6 Reacting to Player Input

September 2015 Calendar This Excel calendar is blank & designed for easy use as a planner. Courtesy of WinCalendar.com

ECE 15B COMPUTER ORGANIZATION

SYSC 2006 C Winter 2012

Dynamic Allocation of Memory

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

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

CP2 Revision. theme: dynamic datatypes & data structures

Basic Device Management

C for Engineers and Scientists: An Interpretive Approach. Chapter 10: Arrays

Aerospace Software Engineering

YEAR 8 STUDENT ASSESSMENT PLANNER SEMESTER 1, 2018 TERM 1

Transcription:

Complex data structures Cedric Saule cedric.saule@uni-bielefeld.de

Arrays in language C In language C, arrays are contiguous spaces in memory. We make the difference between two kinds of arrays : Static arrays : their size is defined during the compilation. Dynamic arrays : their size will be known during the execution. 2

Arrays in memory C Contiguous memory space of exactly same type +1 +1 adress 1 adress 2 adress 3 Size of Type Size of Type Size of Type adress2 adress1 = ((unsigned long) adress1 (unsigned long) adress2) / sizeof(type) adress1 + n = (Type *) ((unsigned long) adress1 + n * sizeof(type)) To acces to the next case in memory : *(adress1 + n) == adress1[n] Like for functions, the array's name identifies the adress of the first cell. 3

Static arrays declaration Declaration of a static array type name[number of elements]; Number of elements as to be known during compilation. Numbers : 1, 5, 10, 100, 12569 Constant : global integers : #define size 10 Constant types are not considered in ANSI. 4

Static arrays declaration Because of the previous considerations, these expressions are equivalent : int t[10]; *t and t[0] (Because the basic pointer is the first cell, the first index is 0.) t and &t[0] *(t + i) and t[i] I neglect here the pointer size conversion, but be careful! 5

Operations on pointers Arrays are contiguous cells in memory, so increasing a pointer by one is pointing the next cell. But : int t[5] ; /* t is a constant pointer. */ int* p; /* p is a variable pointer. */ p = t; p++; /* This operation is legal. */ t = p; t++; /* This operation is illegal because t is a constant */ 6

Operations on pointers C char t[] = ''Hello''; char *p = ''Hello''; 0 1 2 3 4 5 H e l l o \0 char t[] = ''Hello''; ''t'' is a true array. 125 126 127 0 1 2 3 4 5 0 t h \0 H e l l o \0 W char *p = ''Hello''; ''p'' contains the adress of the string beginning among other constants in memory. 7

Static arrays in multidimensions int t[4][3] = { { 1, 2, 3, { 4, 5, 6, { 7, 8, 9, { 10, 11, 12 ; In memory : 1 2 3 4 5 6 7 8 9 10 11 12 According to the previous definition, we have : t[i][j] = *(int*)(t[i] + j) = *(int*)(*(t + i) + j) = *(int*)(*(t + ( i * 3 + j)); Number of cells in the second dimension t [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2] [3][0] [3][1] [3][2] 8

Static arrays in multidimensions Another way : int t[4][3], *u[4]; u is a 4 cells array of integers pointers. [0] [1] [2] [3] u Of course, u[2][1] is a valid expression! Initialisation : int i = 0; for(i = 0; i < 4; i++) { u[i] = t[i]; Value : u[i][j] = *(int*)(*( u + i ) + j ) t [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2] [3][0] [3][1] [3][2] 9

Static arrays in multidimensions initialisation : int t[4][3] = { { 1, 2, 3, { 4, 5, 6, { 7, 8, 9, { 10, 11, 12 ; 1 2 3 4 5 6 7 8 9 10 11 12 int t[4][3] = { { 1, 2, 3, { 4, 5, 6, { 7, 8, 9 ; 1 2 3 4 5 6 7 8 9 0 0 0 int t[4][3] = { { 1, 2, { 4, 5, { 7, 8, { 10, 11 ; 1 2 0 4 5 0 7 8 0 10 11 0 int t[4][3] = { { 1, 2, 3, 4, 5, 6, 7, 8 ; 1 2 3 4 5 6 7 8 0 0 0 0 10

Dynamic arrays In dynamic arrays, the number of cells is known during the execution. It is impossible to use the basic declaration of arrays since the number of cells and is ignored and so, the memory occupancy. But arrays are also pointers, so we can use this definition! 11

Dynamic arrays We reserve memory and store the adress in a pointer. For multidimension, we do the same! But the memory will contain adresses int size1 = 4, size2 = 3, i = 0; float** t; t = malloc(size1 * sizeof(float*)); for( i = 0; i < size1; i++ ) { t[i] = malloc(size2 * sizeof(float)); for(i = 0; i < size1; i++) { free(t); free(t[i]); Free the memory of the array of float* Reservation of size1 cells of memory with the size of a float*. Reservation of size2 cells of memory with the size of a float. Each pointer of size1 cells points to the adress of a pointer of size2 cells. Free the memory of each array of float 12

Dynamic arrays Arrays creation Declaration of float** t 13

Dynamic arrays Arrays creation t Memory allocation of size1 cells of size float* 14

Dynamic arrays Arrays creation t float float float t[0] Memory allocation of size2 cells of float. Allocation to the cell0 of the previous array. 15

Dynamic arrays Arrays creation t float float float float float float float float float t[0] t[1] t[2] float float float t[3] We repeat the operation for each cell of the array of float*. 16

Dynamic arrays Arrays destruction t The memory of each array of size2 cells is freed. Be careful, the pointers point now on nothing! 17

Dynamic arrays Arrays destruction t The size1 memory spaces are freed. t points on nothing. 18

Dynamic arrays Arrays creation t float float float If we free t before each of the size1 array of float. We loose acces to their adress. float float float float float float Anonymous reserved memory float float float 19

Dynamic arrays We can define non homogenous arrays. t float float float t[0] float float float float float float float float float t[1] t[2] float float float t[3] We have just to keep in memory the number of cells for each. 20

Arrays and functions To use an array as a parameter, the pointer on the first adress is enough. Better is float sumarray(float* myarray, int nb1 ) { int i; float sum = 0; for(i = 0; i < nb1; i++) { sum += myarray[i]; return sum; float sumarray(const float* myarray, int nb1 ) { int i; float sum = 0; for(i = 0; i < nb1; i++) { sum += myarray[i]; return sum; We cannot modify the arrays by ''mistake'' 21

Arrays and functions We can also write : float sumarray(const float myarray[], int nb1 ) { int i; float sum = 0; for(i = 0; i < nb1; i++) { sum += myarray[i]; return sum; Or, equivalently : float sumarray(const float myarray[15], int nb1 ) { int i; float sum = 0; for(i = 0; i < nb1; i++) { sum += myarray[i]; return sum; The compiler does not check the bounds! 22

Arrays and functions In two dimensions (or more...) : float sumarray(const float myarray[][nb2], int nb1 ) { int i, j; float sum = 0; for(i = 0; i < nb1; i++) { for(j = 0; j < nb2; j++) { sum += myarray[i][j]; return sum; Or : t Because we have to knoe the dimensions sizes after the first one. Remember : t[i][j] = *(float*)(*(t + ( i * 3 + j)) [0][0] [0][1] [0][2] [1][0] [1][1] [1][2] [2][0] [2][1] [2][2] [3][0] [3][1] [3][2] float sumarray(const * myarray, int nb1, int nb2 ) { int i, j; float sum = 0; for(i = 0; i < nb1; i++) { for(j = 0; j < nb2; j++) { sum += *(float*)(*(myarray + i * nb2 + j)); return sum; 23

Convenient functions calloc : initialise the value to ''0'' or ''NULL'' : int* myarray = calloc(nb, sizeof(int)); realloc : resize dynamically the memory space. myarray = realloc(myarray, (nb+1) * sizeof(int)); These two functions returns NULL in case of mistake. We have to be careful (temporary variables, test of right allocation...) 24

Another way to enumerate We want to use ''strings'' as identifyers. Exemples : Days of the week. Months Each name is associated with an integer : enum day {monday, tuesday, wednesday, thursday, friday, satursday, sunday; Monday is equal to 0, tuesday to 1... enum somemonth {january = 1, february, march, april, september = 9, october, november; 10 11 25

Quick preview How to make a copy of an array in fonction? How to create new data types? What about trees? Graphs? Lists? Stacks? We can define structures which contain several types and recursive definitions. 26