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.

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

An introduction to pointers in c
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.
Programming and Data Structure
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.
Dynamic Memory Allocation
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.
CSCI 171 Presentation 11 Pointers. Pointer Basics.
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.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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.
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.
DYNAMIC MEMORY MANAGEMENT. int x; When the source code containing this statement is compiled and linked, an executable file is generated. When the executable.
Kernighan/Ritchie: Kelley/Pohl:
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:
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
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 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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-
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.
Pointers Applications
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
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,
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
Dynamic memory allocation and Pointers Lecture 4.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Learners Support Publications Operators.
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.
Stack and Heap Memory Stack resident variables include:
Introduction to Programming
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Dynamic Memory Allocation
Programming and Data Structures
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Memory Allocation CS 217.
Dynamic Memory Allocation
Operators.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
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
Presentation transcript:

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 is also loaded into memory for fast access. A memory is made up of a large number of cells, where each cell is capable of storing one bit. The cells may be organized as a set of addressable words, each word storing a sequence of bits. These addressable memory cells should be managed effectively to increase its utilization. That is memory management is to handle request for storage (that is new memory allocations for a variable or data) and release of storage (or freeing the memory) in most effective manner.

MEMORY ALLOCATION IN C There are two types of memory allocations in C: 1. Static memory allocation or Compile time 2. Dynamic memory allocation or Run time In static or compile time memory allocations, the required memory is allocated to the variables at the beginning of the program. Here the memory to be allocated is fixed and is determined by the compiler at the compile time itself. For example int i, j; //Two bytes per (total 2) integer variables float a[5], f; //Four bytes per (total 6) floating point variables

Static memory allocation drawbacks Static memory allocation has following drawbacks. leads to the inefficient use of memory. Insertion cannot exceed the size of array

Dynamic or Run Time Memory Allocation The dynamic or run time memory allocation helps us to overcome this problem. It makes efficient use of memory by allocating the required amount of memory whenever is needed. In most of the real time problems, we cannot predict the memory requirements. Dynamic memory allocation does the job at run time. C provides the following dynamic allocation and de- allocation functions : (i) malloc( ) (ii) calloc( ) (iii) realloc( ) (iv) free( )

malloc( ) The malloc( ) function is used to allocate a block of memory in bytes. The malloc() function returns a pointer of any specified data type after allocating a block of memory of specified size. It is of the form ptr = (int_type *) malloc (block_size) ‘ptr’ is a pointer of any type ‘int_type’ byte size is the allocated area of memory block. For example ptr = (int *) malloc (10 * sizeof (int));

Remember the malloc() function allocates a block of contiguous bytes. The allocation can fail if the space in the heap is not sufficient to satisfy the request. If it fails, it returns a NULL pointer. So it is always better to check whether the memory allocation is successful or not before we use the newly allocated memory pointer.

//THIS IS A PROGRAM TO FIND THE SUM OF n ELEMENTS USING //DYNAMIC MEMORY ALLOCATION #include //Defining the NULL pointer as zero #define NULL 0 void main() { int i,n,sum; //Allocate memory space for two-integer pointer variable int *ptr,*ele; clrscr(); //Clear the screen printf(“\nEnter the number of the element(s) to be added = ”); scanf(“%d”,&n); //Enter the number of elements //Allocating memory space for n integers of int type to *ptr ptr=(int *)malloc(n*sizeof(int)); //Checking whether the memory is allocated successfully if(ptr == NULL) { printf(“\n\nMemory allocation is failed”); exit(0); } //Reading the elements to the pointer variable *ele for(ele=ptr,i=1;ele<(ptr+n);ele++,i++) { printf(“Enter the %d element = ”,i); scanf(“%d”,ele); } //Finding the sum of n elements for(ele=ptr,sum=0;ele<(ptr+n);ele++) sum=sum+(*ele); printf(“\n\nThe SUM of no(s) is = %d”,sum); getch(); }

calloc( ) The calloc( ) function works exactly similar to malloc( ) function except for the fact that it needs two arguments as against one argument required by malloc( ) function. While malloc( ) function allocates a single block of memory space, calloc( ) function allocates multiple blocks of memory, each of the same size, and then sets all bytes to zero. The general form of calloc( ) function is: ptr = (int_type*) calloc(n sizeof (block_size)); ptr = (int_type*) malloc(n* (sizeof (block_size)); For example ptr = (int *) calloc(25, 4); ptr = (int *) calloc(25,sizeof (float)); The memory allocated using malloc() function contains garbage values, the memory allocated by calloc() function contains the value zero.

free() Dynamic memory allocation allocates block(s) of memory when it is required and deallocates or releases when it is not in use. It is important and is our responsibility to release the memory block for future use when it is not in use, using free() function. The free() function is used to deallocate the previously allocated memory using malloc() or calloc() function. The syntax of this function is free(ptr); ‘ptr’ is a pointer to a memory block which has already been allocated by malloc() or calloc() functions. Trying to release an invalid pointer may create problems and cause system crash.

realloc() In some situations, the previously allocated memory is insufficient to run the correct application, i.e., we want to increase the memory space. It is also possible that the memory allocated is much larger than necessary, i.e., we want to reduce the memory space. In both the cases we want to change the size of the allocated memory block and this can be done by realloc() function. This process is called reallocation of the memory. The syntax of this function is ptr = realloc(ptr, New_Size) Where :‘ptr’ is a pointer holding the starting address of the allocated memory block. : And New_Size is the size in bytes that the system is going to reallocate.

realloc() Following example ptr = (int *) malloc(sizeof (int)); ptr = (int *) realloc(ptr, sizeof (int)); ptr = (int *) realloc(ptr, 2); (Both the statements are same) ptr = (int *) realloc(ptr, sizeof (float)); ptr = (int *) realloc(ptr, 4); (Both the statements are same)

Thank You