Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
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.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers.
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Recursion (II) H&K Chapter 10 Instructor – Gokcen Cilingir Cpt S 121 (July 25, 2011) Washington State University.
Structs H&K Chapter 11 Instructor – Gokcen Cilingir Cpt S 121 (July 18, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
CS100A, Fall 1997, Lecture 241 CS100A, Fall 1997 Lecture 24, Tuesday 25 November (There were no written notes for lecture 23 on Nov. 20.) Data Structures.
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.
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.
Pointers Applications
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lecture 13 Static vs Dynamic Memory Allocation
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
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 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
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.
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.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
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.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
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.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Introduction to Programming
CSCI206 - Computer Organization & Programming
(1-1) C Review: Pointers, Arrays, Strings, & Structs
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Dynamic Data Structures
Presentation transcript:

Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University

Dynamic Data Structures (1) Structures that expand and contract as a program executes Using dynamic data structures, you can allocate memory as needed (no need to constraint the maximum size of an array at compile time) Dynamic memory allocation allows us to dynamically allocate an array at run time. Image source:

Dynamic Data Structures (2) When allocating memory dynamically, a program can use the space to store any of the simple and structured data types (i.e. struct s, int s, double s, etc.) We can combine separately allocated structured blocks (called nodes) to form composite structures that expand and contract while the program executes. 527 head A node containing a data field and a pointer to the next node

C. Hundhausen, A. O’Fallon4 A pointer variable contains the address of another cell containing a data value Note that a pointer is “useless” unless we make sure that it points somewhere: ◦ int num = 3, int *nump = # The direct value of num is 3, while the direct value of nump is the address (1000) of the memory cell which holds the 3 Pointer Review I 3 numpnum

C. Hundhausen, A. O’Fallon5 Pointer Review II ReferenceExplanationValue numDirect value of num3 numpDirect value of nump1000 *numpIndirect value of nump3 &numpAddress of nump numpnum

6 Pointers as Function Parameters Recall that we define an output parameter to a function by passing the address (&) of the variable to the function The output parameter is defined as a pointer in the formal parameter list

7 Pointers Representing Arrays and Strings Consider these variable declarations: double gradeList[20]; char name[40]; When we want to pass either of these arrays to functions, we use the array name with no subscript average (gradeList); reverse (name); C interprets the array name as the address of the initial array element, so a whole array is always passed to a function as a pointer! Therefore, if we were passing an array to a function, the corresponding formal parameter might be declared in two ways. So, all of the following prototypes are valid for the above functions: double average (double list[]); double average (double *list); void reverse (char name[]); void reverse (char *name); array declarations pass an array to a function arrays as formal parameters

8 Pointers to Structures Recall that when we have a pointer to a structure, we can use the indirect component selection operator -> to access components within the structure typedef struct { double grade; int age; } Student; int main (void) { Student p1, *struct_ptr; p1.grade = 95; p1.age = 20; struct_ptr = &p1; struct_ptr->x; // Access grade field in Student. }

C. Hundhausen, A. O’Fallon9 malloc function (1) Dynamic memory allocation can be performed with the help of the function malloc() found in malloc() has the following prototype: void *malloc (size_t size); The argument to malloc is a number indicating the amount of memory space needed to be allocated We can apply the sizeof operator to a data type to determine the number (the number represents the number of bytes needed)

C. Hundhausen, A. O’Fallon10 malloc function (2) The statement: malloc (sizeof (int)) allocates exactly enough space to hold one integer A call to malloc returns a pointer to the block of memory allocated (if no memory can be allocated, NULL is returned) The return type is void * which is generic, thus we need to assign the pointer to a specific type by explicitly typecasting: int *nump = (int *) malloc (sizeof (int)); char *chp = (char *) malloc (sizeof (char));

C. Hundhausen, A. O’Fallon, G. Cilingir11 malloc function (3) The area in which the dynamic memory is allocated is called the heap The stack is the area that stores function data when functions are entered ◦ Remember that the memory allocated for a given function's data (on the stack) goes away when the function is exited Function data areaHeap nump chp

C. Hundhausen, A. O’Fallon12 Accessing a Component of a Dynamically Allocated Structure Accessing a component of a dynamically allocated structure is done in the same way as accessing a component of a statically allocated structure: Student *struct_ptr = (Student *) malloc (sizeof (Student)); struct_ptr->grade is equivalent to (*struct_ptr).grade

C. Hundhausen, A. O’Fallon13 Dynamic Array Allocation with calloc calloc() may be used to allocate contiguous blocks of memory for array elements The function is also found in The function prototype for calloc() is as follows: void *calloc (size_t nitems, size_t size); The two arguments to calloc() are the number of array elements needed and the size of one element, respectively.

C. Hundhausen, A. O’Fallon14 calloc As with malloc, calloc also returns a void * to the beginning of the allocated blocks of memory Thus, we can allocate 10 blocks of contiguous memory as follows: Student *struct_ptr = (Student *) calloc (10, sizeof (Student)); Note the above statement is similar to Student array[10]; except that it is allocated dynamically, instead of statically

C. Hundhausen, A. O’Fallon15 Applying Array Notation to Pointers If we executed the calloc statement on the previous slide, we could then use array notation to access each element in the block of dynamically allocated memory: ◦ struct_ptr[0] would access the first element in the array and struct_ptr[9] would access the last element in the array

C. Hundhausen, A. O’Fallon16 Freeing Allocated Memory We need to be able to give up memory after we are done using it We can use the function free() to return the allocated memory to the heap For example: free (struct_ptr); returns the dynamically-allocated memory block pointed to by struct_ptr Memory leaks occur if we do not free memory before the local variable that accesses the allocated memory goes out of scope Memory leaks are a common source of run-time program bugs; they can also slow down a program's execution

17 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.