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

Slides:



Advertisements
Similar presentations
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Advertisements

Introduction to Memory Management. 2 General Structure of Run-Time Memory.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Stack and Heap Memory Stack resident variables include:
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
PHY 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
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.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
UNIT-II Topics to be covered Singly linked list Circular linked list
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
5.13 Recursion Recursive functions Functions that call themselves
Day 03 Introduction to C.
Day 03 Introduction to C.
CSCI206 - Computer Organization & Programming
Pointers and Dynamic Variables
Object Oriented Programming COP3330 / CGS5409
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

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 (ie. A line in the store or at Disneyland)

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 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

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

QUESTIONS?