CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Dynamic memory allocation
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
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.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
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{
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
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.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Chapter 11 Structure and Union Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
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.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Class 2 CSI2172
CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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 ( 台大資工 趙坤茂 )
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,
14/3/02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Structures, ADT Lecture 25 14/3/2002.
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
ECE Application Programming
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Stack and Heap Memory Stack resident variables include:
Introduction to Programming
CSC172 Data Structures Linked Lists (C version)
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Review & Lab assignments
CS111 Computer Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined with a set of basic operations

CP104 Introduction to Programming Structure II Lecture 32 __ 2 Case Study: Complex Number ADT Complex data type typedef struct{ double real, imag; } complex_t; Operations 1.int scan_complex(complex_t *c); 2.void print_complex(complex_t c); 3.complex_t add_complex(complex_t c1, complex_t c2); 4.complex_t subtract_complex(complex_t c1, complex_t c2); 5.complex_t multiply_complex(complex_t c1, complex_t c2); 6.complex_t divide_complex(complex_t c1, complex_t c2); 7.complex_t abs_complex(complex_t c);

CP104 Introduction to Programming Structure II Lecture 32 __ 3 Partial Implementation of Complex ADT (I)

CP104 Introduction to Programming Structure II Lecture 32 __ 4 Partial Implementation of Complex ADT (II)

CP104 Introduction to Programming Structure II Lecture 32 __ 5 Partial Implementation of Complex ADT (III)

CP104 Introduction to Programming Structure II Lecture 32 __ 6 Partial Implementation of Complex ADT (IV)

CP104 Introduction to Programming Structure II Lecture 32 __ 7 Partial Implementation of Complex ADT (V)

CP104 Introduction to Programming Structure II Lecture 32 __ 8 Parallel Array vs. Array of Structures Assume that there are three records first_name last_name ID paymentdate Tom Black Alice Smith Jane White Two ways to store the records –Parallel array: char first_name[3][20]; char last_name[3][20]; int id[3]; double payment[3]; int date[3]; –Array of Structures typedef struct{ char first_name[20]; char first_name[20]; int id; double payment; int date; } customer_t; customer_t record[3]; Advantages and disadvantages

CP104 Introduction to Programming Structure II Lecture 32 __ 9 Structures with Pointer Components typedef struct node { char data; struct node *next; }; The structure type node contains a pointer member of the same type This type of structure can be used to form a linked list data structure A linked list is a sequence of nodes each contains the address of the next node except the last node abcd p

CP104 Introduction to Programming Structure II Lecture 32 __ 10 Use linked list to implement a Stack ADT Node structure type typedef struct node { char data; struct node *next; }; Operations 1.node *create(void); 2.Void freeStack(node *stack); 3.Void push(char inputdat, node *stack); 4.Char pop(node *stack); 5.Char top(node *stack); 6.Int isempty(node *stack); See demo Advantages: assign memory location dynamically –assign when needed, while array can not, must assign fixed length before using it –Nodes of a linked list may not stored in continuous in memory, while an array is stored continuously in memory Disadvantages: Linear access not random access More overhead and relatively slower

CP104 Introduction to Programming Structure II Lecture 32 __ 11 Functions related to memory allocation in stdlib Memory allocation function call will assign memory location to variables, arrays in Heap region of memory. void *malloc(size_t size); –Allocates the requested memory and returns a pointer to it. The requested size is size bytes. The value of the space is indeterminate. On success a pointer to the requested space is returned. On failure a null pointer is returned. void *calloc(size_t nitems, size_t size); –Allocates the requested memory and returns a pointer to it. The requested size is nitems each size bytes long (total memory requested is nitems*size). The space is initialized to all zero bits. On success a pointer to the requested space is returned. On failure a null pointer is returned. void free(void *ptr); –Deallocates the memory previously allocated by a call to calloc, malloc, or realloc. The argument ptr points to the space that was previously allocated. If ptr points to a memory block that was not allocated with calloc, malloc, or realloc, or is a space that has been deallocated, then the result is undefined. No value is returned. sizeof –Is a key word, not a function. e.g. sizeof(int)