Transcript CEG 221: Week 1 Lesson 1
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” • Queues – a line of people • Questions
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
store or at Disneyland) (ie. A line in the
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
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
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*/ }
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 variables to initialize values to “0”; results in smaller executables
memset
for any – 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
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!
Accessing Arrays
• To access an array, use the [] operator: – Indices go from 0 to n-1, where n is the size – The i th position starts from 0, not 1 • The value at position 4 is the 5 th element in the array • The 5 th element of pArray is pArray[4] • Remember when using a dynamically allocated array, check to see if it is NULL
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
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
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;
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)
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);
Next Time
• Advanced Input/Output • Advanced Data Types • Advanced Programming • String Processing using