Chapter 10-1: 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.
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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
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.
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.
User-Level Memory Management in Linux Programming
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:
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{
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.
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 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
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,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
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:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Introduction to Programming
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
C Programming Language Review and Dissection IV
Programmazione I a.a. 2017/2018.
Lecture 6 C++ Programming
This pointer, Dynamic memory allocation, Constructors and Destructor
Some examples.
14th September IIT Kanpur
Circular Buffers, Linked Lists
Programming and Data Structures
Chapter 14 - Advanced C Topics
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
CS111 Computer Programming
Dynamic Memory Allocation
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Homework Continue with K&R Chapter 5 Skipping sections for now
Chapter 9: Pointers and String
C Structures and Memory Allocation
Dynamic Memory – A Review
Pointers, Dynamic Data, and Reference Types
EECE.2160 ECE Application Programming
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

Chapter 10-1: Dynamic Memory Allocation

Introduction Dynamic memory allocation allows our program to obtain more memory space while running, or to release it if it's not required. In simple terms, Dynamic memory allocation allows us to manually handle memory space for your program. Although, C language inherently does not have any technique to allocate memory dynamically, there are 4 library functions under "stdlib.h" for dynamic memory allocation.

Allocating Block of Memory: MALLOC Function available on header file stdlib.h A block of memory may be allocated Allocated block of memory and return a pointer of type void. Means we can assign it to any other type of pointer Syntax ptr_name = (cast_type *)malloc(byte_size); Example 1

Multiple blocks of memory: CALLOC Moemory allocating function normally used for requesting memory space for derived datatype like arrays and structure. Malloc allocates single block where as calloc allocates multiple block Syntax: ptr= (cast_type *) calloc (n, element-size) Example 2

MALLOC vs CALLOC First, is in the number of arguments. malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. calloc() allocates a memory area, the length will be the product of its parameters. calloc fills the memory with ZERO's and returns a pointer to first byte. If it fails to locate enough space it returns a NULL pointer.

Releasing the used space Function free() is used Example 2

Altering the size of Block: Realloc ptr=malloc(size); ptr= relloc(ptr, newsize); The new size may be larger or smaller than the old size Not necessarily add into same block Compiler may find new place and copy old value plus add new space Ex: 3