Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.

Slides:



Advertisements
Similar presentations
Data Structures Static and Dynamic.
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.
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.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
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.
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.
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.
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:
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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,
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
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.
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.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
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.
Object-Oriented Programming in C++
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.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
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.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
STRUCTURE OF A C++ PROGRAM. It is a common practice to organize a program into three separate files. The class declarations are placed in a header file.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Learners Support Publications Operators.
CSC Pointers Powerful feature of the C++ language One of the most difficult to master Essential for construction of interesting data structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Stack and Heap Memory Stack resident variables include:
Operators in c++.
Introduction to Programming
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Operators.
C Programming Pointers
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Introduction of Memory Allocation

Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time or Dynamic allocation (using pointer)

Compile-Time In the first type of allocations, the required amount of memory is allocated to the program element at the start of the program. Here, the memory to be allocated to the variable is fixed and is determined by the compiler at the compile time.

Compile-Time If it is a single integer variable it allocates two bytes to it. If it is an array of five integer values it allocates ten bytes to it. If it is single float type of variable compiler allocation four bytes to it.

Compile-Time For example: int x,y; //x & y variable allocated 2 bytes float a[5]; // 5*4=20 bytes for float array. Here, array variable stored in consecutive (sequential) memory location & other variable are stored randomly at any unknown location in memory.

Compile-Time The main problem with static memory allocation is that if you store less number of elements then the number of elements for which you have declared memory, then the rest of memory will be wasted. so in C, it is not be made available to other application and status is set as allocated and not free. This leads to the inefficient use of memory.

Dynamic Allocation The concept of dynamic allocation helps us to overcome this problem in arrays, as will as allows us to be able to get the required chunk of memory at run-time. This is best suited type of allocation where we do not know the memory requirement in advance, which is the case with most of real-time problem.

Dynamic Allocation In other words, dynamic memory allocation gives flexibility for programmer. As well as it make efficient use of memory, by allocating the required amount of memory whenever needed unlike static allocation where we declare the amount to be allocated statically.

Dynamic Allocation C provide following dynamic allocation and de- allocation functions: malloc() calloc() free() realloc()

Dynamic Allocation C++ provide following dynamic allocation and de- allocation Operators are: new delete

Memory Management Operators: new In C++ supports dynamically memory allocation and free the memory allocation by using two unary operators new and delete that perform the task in better and easier way. An object can be created by using new and destroyed by using delete as and when required. A data object created inside a block with new will remain in existence until it its explicitly destroyed by using delete.

Memory Management Operators: new The new operator can be used to create objects of any type. It takes the following general form: pointer variable=new data-type Here, pointer-variable is a pointer of type data- type. The new operator allocates sufficient memory to hold a data object of type data-type and returns the address of the object.

Memory Management Operators: new The data-type may be any valid data type. The pointer variable hold the address of the memory space allocated examples: int *p=new int; flaot *q=new float; Where p is a pointer of type int and q is a pointer of type float.

Memory Management Operators: new The new can be used to create a memory space for any data type including user-defined type such as arrays, structures and classes. General form of the one-dimensional array is: pointer-variable= new data-type[size] Here size specifies the number of elements in the array.

Memory Management Operators: new For example the statement : int *p=new int[10]; Creates a memory space for an array of 10 integer. P[0] will refer to the first element,p[1] to second element and so on. When creating multi-dimension arrays with new all the array sizes must by supplied: int *p=new int[10][5]; int *q=new int[5][3][2];

Memory Management Operators: delete When a data object is no longer needed, it is destroyed to realease the memory space for reuse. The general form of its use is: delete pointer-variable; The pointer-variable is the pointer that points to a data object created with new. Examples: delete p; delete q;

Memory Management Operators: delete If we want to free a dynamically allocated array, we must use the following form of delete: delete [size] pointer-variable; The size specifies the number of elements in the array to be freed. The problem with this form is that the programmer should remember the size of the array.

Memory Management Operators: delete For example: delete []p; Through this statement it will delete the entire array pointed to by p.