Dynamic Memory Allocation

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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 Allocation (also see pointers lectures) -L. Grewe.
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.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 C Review and Dissection V: Dynamic Memory Allocation and Linked Lists These lecture notes created by Dr. Alex Dean, NCSU.
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.
Informática II Prof. Dr. Gustavo Patiño MJ
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 Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
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.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
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,
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.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
6-1 Embedded Systems C Programming Language Review and Dissection IV Lecture 6.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
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.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
Dynamic Allocation Review Structure and list processing
Day 03 Introduction to C.
Introduction to Programming
Dynamic Memory Allocation Strings
ENEE150 Discussion 07 Section 0101 Adam Wang.
2016.
Dynamic Memory Allocation
C Programming Language Review and Dissection IV
Pointers and Memory Overview
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
CS157: Dynamic Memory Dynamic Memory 11/9/201804/25/06.
CSCI206 - Computer Organization & Programming
Programming and Data Structures
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
CSC215 Lecture Memory Management.
Memory Allocation CS 217.
By Hector M Lugo-Cordero September 17, 2008
Dynamic Memory A whole heap of fun….
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
Dynamic Memory A whole heap of fun….
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
Chapter 10-1: Dynamic Memory Allocation
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
Run-time environments
SPL – PS2 C++ Memory Handling.
Dynamic Data Structures
Presentation transcript:

Dynamic Memory Allocation malloc, calloc, free

Program Memory Structure Local memory allocated on the stack Global memory allocated on the heap Static and global allocation of memory for variables is performed at compile and link time. Global Local HEAP STACK CODE

Why Dynamic? Dynamic memory allocation allows new memory to be assigned, used, and then freed (unassigned) while the program is running. This is called dynamic because these runtime variables are not permanent and can be created and destroyed at any time in the program. The memory for the dynamic variables is allocated from the unused areas of the heap. Since a block of memory is allocated for use from the heap, that block of memory is referred to by a memory address. This means that dynamic memory variables require pointers. Dynamic memory allocation is commonly used when dealing with arrays that are not a fixed size. For example, the array size can increase, when more elements are inserted (added), or the size can decrease when elements are deleted (removed).

C and Dynamic Memory C provides 3 functions to allocate dynamic memory and free memory. These functions automatically manage the heap for you. The functions are malloc(), calloc() and free(). The include file required is <stdlib.h> C automatically manages the heap for you. Therefore you only have to tell C how much space to allocate and it finds out where there is enough unused memory, flags this as used, and returns a pointer pointing to the block of memory allocated.

Space allocated by malloc() void *malloc(size) If malloc() could find space for size bytes of memory in the heap, then it returns a pointer to the block in memory. If malloc() could not find space for size bytes of memory in the heap, then it returns a NULL. You must always check for this condition when allocating memory. Pointer to allocated memory Space allocated by malloc() HEAP STACK CODE

malloc() example code fragment #include <stdio.h> #include <stdlib.h> int main(void) { int *i_ptr ; /* allocate memory for 20 integers*/ i_ptr = (int *)malloc(20 * sizeof(int) ) ; if (i_ptr == NULL) printf("Not enough memory to allocate integers\n"); exit(-1); /* terminate program if out of memory */ } /****** REST of Your Program ******/

Space allocated by calloc() void *calloc(n_items,size) If calloc() could find space for array of n_items*size bytes of memory in the heap, then it returns a pointer to the block in memory. If malloc() could not find space for n_items*size bytes of memory in the heap, then it returns a NULL. You must always check for this condition when allocating memory. Pointer to allocated memory Space allocated by calloc() HEAP STACK CODE

calloc() example code fragment int *i_ptr ; /* allocate memory for 20 integers*/ i_ptr = (int *)calloc(20, sizeof(int) ) ; if (i_ptr == NULL) { printf("Not enough memory to allocate integers\n"); exit(-1); /* terminate program if out of memory */ }

void *free(address_of_memory_block) free() deallocates the memory block created by a previous call to malloc() or calloc(). This frees up the memory so that it can be dynamically allocated again if required. free() can only be used with the malloc() or calloc(). The size of the memory block is not required because the heap memory manager knows the size of the memory block from the address given. When the memory is allocated the heap memory manager remembers the address and the size.

Allocate a 10 char string using malloc() #include <stdio.h> #include <stdlib.h> int main(void) { char *str; /* allocate memory for string */ if ((str = (char *) malloc(10*sizeof(char))) == NULL) { /* terminate program if out of memory */ printf("Not enough memory to allocate buffer\n") ; exit(1); } strcpy(str, "Hello") ; // copy "Hello" into string printf("String is %s\n", str) ; // display string /* free allocated memory */ free(str) ;

Allocate a 10 char string using calloc() #include <stdio.h> #include <stdlib.h> int main(void) { char *str; /* allocate memory for string */ if ((str = (char *) calloc(10,sizeof(char))) == NULL) { /* terminate program if out of memory */ printf("Not enough memory to allocate buffer\n") ; exit(1); } strcpy(str, "Hello") ; // copy "Hello" into string printf("String is %s\n", str) ; // display string /* free allocated memory */ free(str) ;