Lecture 13 Static vs Dynamic Memory Allocation

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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
CSC 270 – Survey of Programming Languages C Lecture 6 – Pointers and Dynamic Arrays Modified from Dr. Siegfried.
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.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
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.
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.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
ספטמבר 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.
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.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
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{
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
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 Chapter 9 Arrays and Pointers. 2  One-dimensional arrays  The Relationship between Arrays and Pointers  Pointer Arithmetic and Element Size  Passing.
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.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Pointers Applications
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
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,
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.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
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.
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.
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.
Stack and Heap Memory Stack resident variables include:
Introduction to Programming
2016.
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Pointers.
Programming and Data Structures
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Pointers.
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

Lecture 13 Static vs Dynamic Memory Allocation malloc(), calloc(), free() Reading Assignments: Chapter 9, Section 9

Static Memory Allocation If the size of array is known before compilation, we do Memory a[1] a[2] a[0] int a[3]; How about if the number of elements we want is unknown?

Dynamic Memory Allocation int *a = NULL; int numbers; printf(“How many elements you want?\n”); scanf( “%d”, &numbers); a /* memory allocation */ a = (int *) malloc (numbers*sizeof(int)); a Memory

Dynamic Memory Allocation malloc calloc Take one parameter: void *malloc(object_size); Take two parameters: void *calloc(n, object_size); Return a pointer to the starting address of the allocated memory Or Return NULL Storage is not initialized Storage is initialized to zero

Dynamic memory allocation In order to acquire storage during program execution, C provides calloc() and malloc() (available in stdlib.h) for dynamic memory allocation To allocate a contiguous memory space for n object of a type, use calloc(n, object_size) calloc() returns a pointer to the starting address of the allocated memory if enough memory can be found; otherwise, a NULL value is returned; the storage gained is initialized to zero

Dynamic memory allocation (cont’) malloc() returns a pointer to the starting address of the allocated memory if enough memory can be found; otherwise, a NULL value is returned; its format is malloc(object_size) The function prototypes of calloc() and malloc() are void *malloc(size_t) void *calloc(int, size_t) The storage set aside by malloc() is NOT initialized

Dynamic memory allocation (cont’) As the pointers returned by malloc() and malloc() is of type void *, they can be assigned to other pointers without casting Space allocated by calloc() and malloc() is not released on function exit (but it is released on program exit) and has to be released by calling a system library function free() (available in stdlib.h) The prototype for free() is void free(void *ptr);

Dynamic memory allocation (cont’) Since the size of a data object of a particular data type is machine-dependent, sizeof() is often used when malloc() or calloc() is called, e.g., int *a; … /* get memory space for 5 integers */ if ((a = (int *) calloc(5, sizeof(int)))==NULL) { printf(“calloc() failed\n”); exit(1); } ... free(a); /* free the space */

Dynamic Memory Allocation int *a = NULL; int i, numbers; printf(“How many elements you want?\n”); scanf( “%d”, &numbers); /* allocate memory space */ if ((a=(int *) malloc(numbers*sizeof(int))) == NULL){ printf(“Not Enough Memory Space!\n”); exit(-1); /* end the program */ } for (i = 0; i < numbers; i++) a[i] = i*i; /* free memory space */ free(a);