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.
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.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
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.
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.
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 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
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.
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
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,
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
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 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
CS102 Introduction to Computer Programming Chapter 9 Pointers.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
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.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
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.
C Programming Types & Dynamic memory & bits & others
Memory allocation & parameter passing
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Day 03 Introduction to C.
CSE 374 Programming Concepts & Tools
Computer Science 210 Computer Organization
Lesson One – Creating a thread
ENEE150 Discussion 07 Section 0101 Adam Wang.
2016.
Programmazione I a.a. 2017/2018.
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
14th September IIT Kanpur
CSC 253 Lecture 8.
Programming and Data Structures
CSC 253 Lecture 8.
Computer Science 210 Computer Organization
Pointers, Dynamic Data, and Reference Types
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Chien-Chung Shen CIS/UD
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Dynamic Memory Allocation In this diagram of a program’s memory, the heap is a pool of available memory that can be allocated dynamically while your program is running. Whereas a function's local (i.e. stack) variables disappear when the function returns, memory allocated from the heap will persist until it is explicitly given back to the operating system.

A call to malloc() prototype: void* malloc(size in bytes); example: The C library provides a function called malloc() (short for memory allocation) that allocates a block of memory from the heap and returns a pointer to it. Whenever your program needs fresh storage at run time that must outlast the current function, it can call malloc(), passing it the number of bytes needed. Since the sizes of data types vary from system to system, C provides the sizeof() operator for determining the size of a particular type. In this example, sizeof(int) * 10 evaluates to 40 bytes on the appliance, so we are asking the OS for a pointer to 40 bytes of memory. **** Note that sizeof() is considered an operator not a function because it takes any kind of data type and returns the appropriate size, whereas a function must specify what type of argument it takes. **** prototype: void* malloc(size in bytes); example: int* ptr = malloc(sizeof(int) * 10);

Check for NULL! int* ptr = malloc(sizeof(int) * 10); if (ptr == NULL) Normally malloc() returns a pointer to a block of newly allocated memory from the heap. However, if there is an error, or the heap has been exhausted, malloc() returns NULL. For this reason, ALWAYS CHECK THE POINTER RETURNED BY MALLOC!! If the returned pointer is NULL, you must handle this case by signaling an error. Programs that don't perform this check can dereference NULL, which results in a segmentation fault. int* ptr = malloc(sizeof(int) * 10); if (ptr == NULL) { printf("Error -- out of memory.\n"); return 1; }

A call to free() prototype: void free(pointer to heap memory); When a program has finished using heap memory, it should release the storage by calling free() on the pointer that was originally produced by malloc(). All memory that is malloc'd must later be free'd (but only once), and only memory that was produced by malloc() should be free'd. prototype: void free(pointer to heap memory); example: free(ptr);

#include <stdio.h> #include <cs50.h> int main(void) { int* ptr = malloc(sizeof(int)); if (ptr == NULL) printf("Error -- out of memory.\n"); return 1; } *ptr = GetInt(); printf("You entered %d.\n", *ptr); free(ptr); Let's walk through this program: First, we ask the OS for a pointer to 4 bytes of memory in the heap. Next, we check to make sure malloc() did not return NULL. We then get an integer from the user, store it in the memory that malloc() gave us, and print the integer. Finally, we free the malloc'd memory.