Computer Science: A Structured Programming Approach Using C1 10-4 Memory Allocation Functions C gives us two choices when we want to reserve memory locations.

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

Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Symbol Table.
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.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Chapter 6 Data Types
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Dynamic Memory Allocation The memory usage for program data can increase or decrease as your program runs. The memory usage for program data can increase.
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
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.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
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.
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:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
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.
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.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Memory Allocation Ming Li Department.
Chapter 10 Storage Management Implementation details beyond programmer’s control Storage/CPU time trade-off Binding times to storage.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
Pointers Applications
Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand design concepts for fixed-length and variable- length strings ❏
1 MT258 Computer Programming and Problem Solving Unit 7.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Lecture 13 Static vs 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:
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: A Structured Programming Approach Using C1 4-6 Scope Scope determines the region of the program in which a defined object is visible.
Week 9 Part 1 Kyle Dewey. Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2.
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.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
Lecture by: Prof. Pooja Vaishnav.  Language Processor implementations are highly influenced by the kind of storage structure used for program variables.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
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.
POINTERS. // Chapter 3 - Program 1 - POINTERS.CPP #include int main() { int *pt_int; float *pt_float; int pig = 7, dog = 27; float x = , y = 32.14;
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
CSC Programming for Science Lecture 34: Dynamic Pointers.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CS 241 Discussion Section (02/02/2012). MP2 Overview  Task is simple  Reimplement malloc(), calloc(), realloc() and free()  A contest will be running.
Learners Support Publications Operators.
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
CSCI206 - Computer Organization & Programming
Chapter 9 Pointers Objectives
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Dynamic Memory Allocation
Memory Allocation Functions
Topics discussed in this section:
By Hector M Lugo-Cordero September 17, 2008
Topics discussed in this section:
Chapter 10-1: Dynamic Memory Allocation
Presentation transcript:

Computer Science: A Structured Programming Approach Using C Memory Allocation Functions C gives us two choices when we want to reserve memory locations for an object: static allocation and dynamic allocation. Memory Usage Static Memory Allocation Dynamic Memory Allocation Memory Allocation Functions Releasing Memory (free) Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C2 FIGURE Memory Allocation

Computer Science: A Structured Programming Approach Using C3 FIGURE A Conceptual View of Memory

Computer Science: A Structured Programming Approach Using C4 We can refer to memory allocated in the heap only through a pointer. Note

Computer Science: A Structured Programming Approach Using C5 FIGURE Accessing Dynamic Memory

Computer Science: A Structured Programming Approach Using C6 FIGURE Memory Management Functions

Computer Science: A Structured Programming Approach Using C7 Memory Allocation Casting Prior to C99, it was necessary to cast the pointer returned from a memory allocation function. While it is no longer necessary, it does no harm as long as the cast is correct. If you should be working with an earlier standard, the casting format is: pointer = (type*) malloc(size) Note

Computer Science: A Structured Programming Approach Using C8 FIGURE malloc

Computer Science: A Structured Programming Approach Using C9 FIGURE calloc

Computer Science: A Structured Programming Approach Using C10 FIGURE realloc

Computer Science: A Structured Programming Approach Using C11 FIGURE Freeing Memory

Computer Science: A Structured Programming Approach Using C12 Using a pointer after its memory has been released is a common programming error. Guard against it by clearing the pointer. Note

Computer Science: A Structured Programming Approach Using C13 The pointer used to free memory must be of the same type as the pointer used to allocate the memory. Note

Computer Science: A Structured Programming Approach Using C Array of Pointers Another useful structure that uses arrays and pointers is an array of pointers. This structure is especially helpful when the number of elements in the array is variable.

Computer Science: A Structured Programming Approach Using C15 Table 10-2A Ragged Table

Computer Science: A Structured Programming Approach Using C16 FIGURE A Ragged Array

Computer Science: A Structured Programming Approach Using C Programming Applications This section contains two applications. The first is a rewrite of the selection sort, this time using pointers. The second uses dynamic arrays. Selection Sort Revisited Dynamic Array Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C18 FIGURE Selection Sort with Pointers—Structure Chart

Computer Science: A Structured Programming Approach Using C19 PROGRAM 10-4Selection Sort Revisited

Computer Science: A Structured Programming Approach Using C20 PROGRAM 10-4Selection Sort Revisited

Computer Science: A Structured Programming Approach Using C21 PROGRAM 10-4Selection Sort Revisited

Computer Science: A Structured Programming Approach Using C22 PROGRAM 10-4Selection Sort Revisited

Computer Science: A Structured Programming Approach Using C23 PROGRAM 10-4Selection Sort Revisited

Computer Science: A Structured Programming Approach Using C24 PROGRAM 10-4Selection Sort Revisited

Computer Science: A Structured Programming Approach Using C25 PROGRAM 10-4Selection Sort Revisited

Computer Science: A Structured Programming Approach Using C26 FIGURE Dynamic Array Structure Chart

Computer Science: A Structured Programming Approach Using C27 FIGURE Ragged Array Structure

Computer Science: A Structured Programming Approach Using C28 PROGRAM 10-5Dynamic Arrays: main

Computer Science: A Structured Programming Approach Using C29 PROGRAM 10-5Dynamic Arrays: main

Computer Science: A Structured Programming Approach Using C30 PROGRAM 10-6Dynamic Arrays: buildTable

Computer Science: A Structured Programming Approach Using C31 PROGRAM 10-5Dynamic Arrays: buildTable

Computer Science: A Structured Programming Approach Using C32 PROGRAM 10-7Dynamic Arrays: fillTable

Computer Science: A Structured Programming Approach Using C33 PROGRAM 10-7Dynamic Arrays: fillTable

Computer Science: A Structured Programming Approach Using C34 PROGRAM 10-8Dynamic Arrays: Process Table

Computer Science: A Structured Programming Approach Using C35 PROGRAM 10-8Dynamic Arrays: Process Table

Computer Science: A Structured Programming Approach Using C36 PROGRAM 10-9Dynamic Arrays: Find Row Minimum

Computer Science: A Structured Programming Approach Using C37 PROGRAM 10-9Dynamic Arrays: Find Row Minimum

Computer Science: A Structured Programming Approach Using C38 PROGRAM 10-10Dynamic Arrays: Find Row Maximum

Computer Science: A Structured Programming Approach Using C39 PROGRAM 10-11Dynamic Arrays: Find Row Average

Computer Science: A Structured Programming Approach Using C40 PROGRAM 10-12Dynamic Arrays: Find Smaller

Computer Science: A Structured Programming Approach Using C41 PROGRAM 10-13Dynamic Arrays: Find Larger

Computer Science: A Structured Programming Approach Using C Software Engineering Pointer applications need careful design to ensure that they work correctly and efficiently. The programmer not only must take great care in the program design but also must carefully consider the data structures that are inherent with pointer applications. Pointers and Function Calls Pointers and Arrays Array Index Commutativity Dynamic Memory: Theory versus Practice Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C43 Whenever possible, use value parameters. Note

Computer Science: A Structured Programming Approach Using C44 PROGRAM 10-14Testing Memory Reuse

Computer Science: A Structured Programming Approach Using C45 PROGRAM 10-14Testing Memory Reuse