16
CEG 221 Lesson 2: Homogeneous Data Types Mr. David Lippa

CEG 221 Lesson 2: Homogeneous Data Types

Embed Size (px)

DESCRIPTION

CEG 221 Lesson 2: Homogeneous Data Types. Mr. David Lippa. Overview. Review of Homogeneous Data Types & Their Applications Arrays Data structures that we will briefly cover today, and in more detail later in the term: Lists – an ordinary unordered list Stacks – a “stack of plates” - PowerPoint PPT Presentation

Citation preview

Page 1: CEG 221 Lesson 2: Homogeneous Data Types

CEG 221Lesson 2: Homogeneous Data Types

Mr. David Lippa

Page 2: CEG 221 Lesson 2: Homogeneous Data Types

Overview

• Review of Homogeneous Data Types & Their Applications– Arrays– Data structures that we will briefly cover

today, and in more detail later in the term:• Lists – an ordinary unordered list• Stacks – a “stack of plates”• Queues – a line of people

• Questions

Page 3: CEG 221 Lesson 2: Homogeneous Data Types

What is a Homogeneous Data Type?

• A Homogeneous data type is a data type that is comprised solely of one type of data– Examples:

• An array of integers (just a collection)• An (un)ordered list of integers• A stack of struct ProcessType* (an OS

environment of running processes)• A queue of struct PersonType (ie. A line in the

store or at Disneyland)

Page 4: CEG 221 Lesson 2: Homogeneous Data Types

Arrays

• Arrays are a homogeneous data type that is the most basic collection of members of the same data types in chunks– Statically Allocated – hard-coded number of elements

• Example: double pArray[15];

– Dynamically Allocated – variable number of elements, with malloc/free (from stdlib.h)

• Example: double *pArray = malloc( 15 * sizeof(double) );

• Both examples are 15 doubles – but you have to allocate memory for everything you are storing at once time; cannot grow when full

Page 5: CEG 221 Lesson 2: Homogeneous Data Types

Statically Allocated Arrays

• The number of members allocated in a statically allocated array is constant, determined at compile time.

• Advantages: easy to initialize arrays of basic types to “0”; memory freed when array goes out of scope; results in larger executables– double pArray[15] = {0};

• Disadvantages: fixed number of elements (using a constant); always allocates memory whether it is used or not

Page 6: CEG 221 Lesson 2: Homogeneous Data Types

Creating Statically Allocated Arrays: Examples

• An initialized array of 15 ints:– int pArray[15] = {0}; // each int initialized to 0

• A character string of 15 characters:– char pArray [16] = { ‘\0’ };– always null terminated with ‘\0’

• An initialized array of 15 people:– struct PersonType pArray [15]; // allocate– for (i = 0; i < 15; i++) { /* initialize values*/ }

Page 7: CEG 221 Lesson 2: Homogeneous Data Types

Dynamically Allocated Arrays

• The number of members allocated in a dynamically allocated array can be determined by any value – a constant or a variable.

• Advantages: requires a memset for any variables to initialize values to “0”; results in smaller executables– double *pArray = malloc( 15 * sizeof(double) ); // decl– memset( pArray, 0, 15 * sizeof(double) ); // init

• Disadvantages: programmer must free memory when done or results in a memory leak; poor knowledge of pointers results in unstable code

Page 8: CEG 221 Lesson 2: Homogeneous Data Types

Creating Dynamically Allocated Arrays: Examples

• An initialized array of 15 ints:– int *pArray = malloc( 15 * sizeof(int) );– memset( pArray, 0, 15 * sizeof(int) );

• A character string of 15 characters:– char *pArray = malloc(16);– memset( pArray, 0, 16 );

• An initialized array of 15 people:– struct PersonType *pPerson =

malloc( 15 * sizeof(struct PersonType) );– for (i = 0; i < 15; i++) { /* initialize values*/ }

• Remember to free(pArray) when done and checking to see if pArray is NULL (allocation failed) before using it!

Page 9: CEG 221 Lesson 2: Homogeneous Data Types

Accessing Arrays

• To access an array, use the [] operator:– Indices go from 0 to n-1, where n is the size– The ith position starts from 0, not 1

• The value at position 4 is the 5th element in the array

• The 5th element of pArray is pArray[4]

• Remember when using a dynamically allocated array, check to see if it is NULL

Page 10: CEG 221 Lesson 2: Homogeneous Data Types

Reading/Writing Statically Allocated Arrays

• To write a statically allocated character array to disk:

char buf[360] = {‘0’};

fwrite(&buf[0], sizeof(char), 360, pOutputFile);

• Remember statically allocated arrays– are not accessible when out of scope

– Are always passed by reference

Page 11: CEG 221 Lesson 2: Homogeneous Data Types

Reading/Writing Dynamically Allocated Arrays

– fwrite(buf, sizeof(char), 360, pOutputFile);• buf is a void*• sizeof(TYPE)• 360 – the number of items being written• pOutputFile – output stream

• Remember– that all arrays are POINTERS– to check for ordinary pointers (ie. char*) if they are

NULL prior to using them

Page 12: CEG 221 Lesson 2: Homogeneous Data Types

Deleting Dynamically Allocated Arrays

• Dynamically allocated arrays are not deleted for you, as other types are

• You must instruct the program to delete it with the command free and then set it to NULL so that no other function can dereference a NULL pointer (results in crash)

• Example:free(pPerson);

pPerson = NULL;

Page 13: CEG 221 Lesson 2: Homogeneous Data Types

Putting it all together

• Declare an array (& allocate memory if needed)• Initialize its values to 0 (or some empty value)

…• Set the elements of the array• Use the elements of the array

…• (When done, free memory if needed)

Page 14: CEG 221 Lesson 2: Homogeneous Data Types

Arrays: Putting it all together{

int numElements = 5;

int *pIntList = malloc( numElements * sizeof(int) );

double pDoubList[5] = {0};

int i = 0;

// populate both lists with needed values

// use both arrays

free(pIntList);

}

Page 15: CEG 221 Lesson 2: Homogeneous Data Types

Next Time

• Advanced Input/Output

• Advanced Data Types

• Advanced Programming

• String Processing using <string.h>

Page 16: CEG 221 Lesson 2: Homogeneous Data Types

QUESTIONS?