16.216 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: 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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
COMP 1402 Winter 2009 Tutorial #8 malloc / calloc / realloc.
Dynamically Allocated Memory String CGS 3460, Lecture 33 Apr 3, 2006 Hen-I Yang.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Pointers A pointer is a reference to another variable (memory location) in a program –Used to change variables inside a function (reference parameters)
CSCI 171 Presentation 11 Pointers. Pointer Basics.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
User-Level Memory Management in Linux Programming
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Dynamic Memory Management CAS CS210 Ying Ye Boston University.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
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.
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.
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{
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #5 More about.
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’;
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Lecture 13 Static vs Dynamic Memory Allocation
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
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.
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE Application Programming
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
ECE Application Programming
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
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.
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
CSE 220 – C Programming malloc, calloc, realloc.
Stack and Heap Memory Stack resident variables include:
ECE Application Programming
ECE Application Programming
Introduction to Programming
ECE Application Programming
Programmazione I a.a. 2017/2018.
Dynamic Memory Allocation
Programming Languages and Paradigms
Some examples.
Programming and Data Structures
Pointers and dynamic memory
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
Dynamic Memory Allocation
EECE.2160 ECE Application Programming
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
C Programming Lecture-8 Pointers and Memory Management
Dynamic Memory – A Review
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation

Lecture outline Announcements/reminders  Program 9 due 12/5  Program 10 to be posted, due 12/10  Will drop lowest grade Today’s class: Dynamic memory allocation  Allocation functions malloc() calloc() realloc()  Type casting  Deallocating memory using free()  Dynamic memory allocation and arrays 5/7/2015 ECE Application Programming: Lecture 31 2

Justifying dynamic memory allocation Data structures (i.e., arrays) usually fixed size  Array length set at compile time  Can often lead to wasted space May want ability to:  Choose amount of space needed at run time Allows program to determine amount  Modify size as program runs Data structures can grow or shrink as needed Dynamic memory allocation allows above characteristics 5/7/2015 ECE Application Programming: Lecture 31 3

Allocation functions (in ) All return pointer to allocated data of type void * (no base type—just an address)  Must cast to appropriate type Arguments of type size_t : unsigned integer Basic block allocation: void *malloc(size_t size); Allocate block and clear it: void *calloc(size_t nmemb, size_t size); Resize previously allocated block: void *realloc(void *ptr, size_t size); 5/7/2015 ECE Application Programming: Lecture 31 4

Basic allocation with malloc() void *malloc(size_t size); Allocates size bytes; returns pointer  Returns NULL if unsuccessful Example: int *p; p = malloc(10000); if (p == NULL) { /* Allocation failed */ } 5/7/2015 ECE Application Programming: Lecture 31 5

Type casting All allocation functions return void * Automatically type cast to appropriate type Can explicitly perform type cast: int *p; p = (int *)malloc(10000); Some IDEs (including Visual Studio) strictly require type cast 5/7/2015 ECE Application Programming: Lecture 31 6

Allocating/clearing memory: calloc() void *calloc(size_t nmemb, size_t size); Allocates (nmemb * size) bytes Sets all bits in range to 0 Returns pointer ( NULL if unsuccessful) Example: integer array with n values int *p; p = (int *)calloc(n, sizeof(int)); 5/7/2015 ECE Application Programming: Lecture 31 7

Resizing allocated space: realloc() void *realloc(void *ptr, size_t size); ptr must point to previously allocated space Will allocate size bytes and return pointer  size = new block size Rules:  If block expanded, new bytes aren’t initialized  If block can’t be expanded, returns NULL ; original block unchanged  If ptr == NULL, behaves like malloc()  If size == 0, will free (deallocate) space Example: expanding array from previous slide p = (int *)realloc(p, (n+1)*sizeof(int)); 5/7/2015 ECE Application Programming: Lecture 31 8

Deallocating memory: free() All dynamically allocated memory should be deallocated when you are done using it  Returns memory to list of free storage  Once freed, program should not use location Deallocation function: void free(void *ptr); Example: int *p; p = (int *)malloc(10000);... free(p); 5/7/2015 ECE Application Programming: Lecture 31 9

Pitfalls: memory leaks Changing pointers leaves inaccessible blocks Example: p = malloc(1000); q = malloc(1000); p = q; Block originally accessed by p is “garbage”  Won’t be deallocated—wasted space Solution: free memory before changing pointer p = malloc(1000); q = malloc(1000); free(p); p = q; 5/7/2015 ECE Application Programming: Lecture 31 10

Pitfalls: dangling pointers free() doesn’t change pointer  Only returns space to free list Pointer is left “dangling”  Holds address that shouldn’t be accessed Solution: assign new value to pointer  Could reassign immediately (as in previous slide)  Otherwise, set to NULL free(p); p = NULL; 5/7/2015 ECE Application Programming: Lecture 31 11

Application: arrays One common use of dynamic allocation: arrays Can determine array size, then create space  Use sizeof() to get # bytes per element Array notation can be used with pointers int i, n; int *arr; printf("Enter n: "); scanf("%d", &n); arr = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; i++) arr[i] = i; 5/7/2015 ECE Application Programming: Lecture 31 12

Example: what does program print? void main() { int *arr; int n, i; n = 7; arr = (int *)calloc(n, sizeof(int)); for (i = 0; i < n; i++) printf("%d ", arr[i]); printf("\n"); n = 3; arr = (int *)realloc(arr, n * sizeof(int)); for (i = 0; i < n; i++) { arr[i] = i * i; printf("%d ", arr[i]); } n = 6; arr = (int *)realloc(arr, n * sizeof(int)); for (i = 0; i < n; i++) { arr[i] = 10 - i; printf("%d ", arr[i]); } 5/7/2015 ECE Application Programming: Lecture 31 13

Next time More applications of dynamic memory allocation  Strings (and arrays of strings)  Multi-dimensional arrays  Data structures Reminders:  Program 9 due 12/5  Program 10 to be posted, due 12/10 5/7/2015 ECE Application Programming: Lecture 31 14