Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6C Pointers,

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.
Malloc Recitation By sseshadr. Agenda Macros in C Pointer declarations Casting and Pointer Arithmetic Malloc.
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.
Dynamic Memory Management
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chapter 6 Data Types
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts / : Introduction to Computer Systems 18 th Lecture, March 24, 2015 Instructors:
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.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
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.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
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.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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{
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 Optimizing Malloc and Free Professor Jennifer Rexford COS 217 Reading: Section 8.7 in K&R book
Malloc Recitation Section K (Kevin Su) November 5 th, 2012.
Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Heap Management.
5/23/20151 GC16/3011 Functional Programming Lecture 19 Memory Allocation Techniques.
CPSC 388 – Compiler Design and Construction
Lab 3: Malloc Lab. “What do we need to do?”  Due 11/26  One more assignment after this one  Partnering  Non-Honors students may work with one other.
OS Fall’02 Memory Management Operating Systems Fall 2002.
1 Optimizing Malloc and Free Professor Jennifer Rexford
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal.
Pointers Applications
Memory Allocation CS Introduction to Operating Systems.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
CS4432: Database Systems II Record Representation 1.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
CS 241 Discussion Section (11/17/2011). Outline Review of MP7 MP8 Overview Simple Code Examples (Bad before the Good) Theory behind MP8.
CS 241 Section Week #9 (11/05/09). Topics MP6 Overview Memory Management Virtual Memory Page Tables.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Malloc Lab : Introduction to Computer Systems Recitation 11: Nov. 4, 2013 Marjorie Carlson Recitation A.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Lecture 7 Page 1 CS 111 Summer 2013 Dynamic Domain Allocation A concept covered in a previous lecture We’ll just review it here Domains are regions of.
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.
Dynamic Memory Management Jennifer Rexford 1. 2 Goals of this Lecture Dynamic memory management techniques Garbage collection by the run-time system (Java)
BIL 104E Introduction to Scientific and Engineering Computing Lecture 9.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Chapter 17 Free-Space Management
Dynamic Memory Allocation
Data Structures Interview / VIVA Questions and Answers
Main Memory Management
Dynamic Memory Allocation Reference Variables
Circular Buffers, Linked Lists
CS 240 – Lecture 21 Alloc Implementation.
Optimizing Malloc and Free
CS Introduction to Operating Systems
Memory Allocation Functions
Memory Management Chapter 10 11/24/2018 Crowley OS Chap. 10.
CS703 - Advanced Operating Systems
Optimizing Dynamic Memory Management
Discussions on hw7 Memory Allocation Exercise 3 functions:
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
LINKED LIST Dr. T. Kokilavani Assistant Professor
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Module 13 Dynamic Memory.
Presentation transcript:

Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #6C Pointers, and a bit More

Programming III Remember to Check – –

Remember malloc calloc realloc free

Example (8.7) Storage Allocator

Storage Allocator Example Uses First Fit – Best fit will look for smallest chunk needed. – First fit looks for first space that has enough space If exact space found, it is unlinked and return to user If the block is too big, it is split – The proper amount is returned to user – Residual is left on the free list If not big enough, another chunk is obtain from OS and linked into the free list

Storage Allocator Example If space being free is adjacent to free block in either side – it is coalesced into single bigger block Most restrictive type is used – Some machines may be a double – And others long or int A free block contains – a pointer to the next block in the chain. – a record of the size of the block – free space If the block is too big, it is split – The proper amount is returned to user – Residual is left on the free list If not big enough, another chunk is obtain from OS and linked into the free list

Align, Size, and More Align field is never used. – Forces each header to be aligned on a worst-case boundary The size field is necessary because the blocks need to be contiguous – Why is not possible to use pointer arithmetic? The block contains one more unit – The header If programmer messes with reserved blocks, – list will be scrambled

Allocation (more) Variable base is used to get started – empty list if freep is NULL, it is the first call to malloc then a degenerate free list is created – Contains one block of size zero. – Points to itself Then List is search The free space is return to the user

malloc The function morecore obtains storage from OS – It is expensive. We want to get at least NALLOC units After setting the size field – morecore inserts the additional memory Unix system call is sbrk(n) – Returns a pointer to n more buts of storage – returns -1 if no space – -1 must be cast to (char *) to compared – This code is portable among machines that general pointer comparison is meaningful

Free Scans free list starting at freep looking for the place to insert free block – Either between two existing blocks or at one end of the list Pointers must be updated to point to correct places

Allocation Example While this allocation is machine dependent – illustrate the concepts This can be used in other situations – Hint (Homework) Suggested Exercises 8-6, 8-7,8-8