malloc(): Dynamic Memory Management

Slides:



Advertisements
Similar presentations
Memory Management Chapter FourteenModern Programming Languages, 2nd ed.1.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Dynamic memory allocation
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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.
The University of Adelaide, School of Computer Science
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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.
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
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:
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’;
CSE 451: Operating Systems Section 1. Why are you here? 9/30/102.
Stacks and HeapsCS-3013 A-term A Short Digression on Stacks and Heaps CS-3013 Operating Systems A-term 2008 (Slides include materials from Modern.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
Intro to Computer Architecture
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Run-time Environment and Program Organization
1 Inner Workings of Malloc and Free Professor Jennifer Rexford COS 217.
Computer Science 210 Computer Organization Pointers and Dynamic Storage.
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
CSc 352 C : Arrays, Structs, Pointers
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
F28PL1 Programming Languages Lecture 9 Programming in C - 4.
CGS 3460 Pointer n To hold the location of another variable n Declaration: la type and a name with an asterisk lE.g. int *ptr; n Assigning a value lptr.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
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:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
CS61C Midterm 1 Review Summer 2004 Pooya Pakzad Ben Huang Navtej Sadhal.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Spring, 2011 –– Computational Thinking – Dennis Kafura – CS 2984 Data Structures Mapping complex structures to linear memory.
F28HS2 Hardware-Software Interface Lecture 4: Programming in C - 4.
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.
Carnegie Mellon 1 Malloc Lab : Introduction to Computer Systems Friday, July 10, 2015 Shen Chen Xu.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Dynamic Storage Allocation
Day 03 Introduction to C.
Computer Science 210 Computer Organization
Lectures linked lists Chapter 6 of textbook
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Computer Science 210 Computer Organization
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Memory Allocation CS 217.
Review & Lab assignments
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
CS111 Computer Programming
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
Class and Objects In a class, all the functions that operate on the data structure are grouped together in one place along with the data Like a struct.
Presentation transcript:

malloc(): Dynamic Memory Management

Csc352-Summer03, Stanley Yao Process Memory Layout Heap meets stack? Access the unused space? low Code Write protected Data BSS Heap unused Stack high Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao Function Calls & Stack fact(2) = 2 int fact(int n) { if (n==0) return 1; else return n*fact(n-1); } n=2 Return address Local variables Argument n=0 Saved registers int fact(int n) { if (n==0) return 1; else return n*fact(n-1); } n=1 Return address Local variables Argument n=1 Saved registers int fact(int n) { if (n==0) return 1; else return n*fact(n-1); } n=0 Return address Local variables Argument n=2 Saved registers Stack Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao malloc() void *malloc(size_t n); Allocate n bytes of memory space in heap Sizeof() is recommended in expressing n Assign the starting address of this space to pointer p If no more space is available, return NULL The returned address from malloc() is void * It’s suggested to cast the returned address to p’s type Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao Example 1 int *values; values = (int *)malloc(3*sizeof(int)); If (value == NULL) { perror(“cs352”); exit(1); } values[0] = 11; values[1] = 13; values[2] = 14; int *value; value = (int *)malloc(sizeof(int)); If (value == NULL) { perror(“cs352”); exit(1); } *value = 10; struct person *p; p = (struct person *) malloc(sizeof(struct person)); If (value == NULL) { perror(“cs352 example”); exit(1); } p->age = 17; Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao Example 2 int *values; values = (int *)malloc(2*sizeof(int)); values[0] = 11; values[1] = 13; values[2] = 14; int *value; value = (int *) malloc(1); *value = 10; struct person *p; p = (struct person *) malloc(sizeof(struct node)); p->age = 17; Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao free() void free(void *ptr); Return the previously allocated memory to the system In Java, GC will do this automatically. However in C, you must do it yourself  Never free a memory: memory leak Free a memory more than once: seg-fault Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao How is Heap Organized? p=malloc(2*sizeof(int)) q=malloc(3*sizeof(int)) free p free q +8 -2 +4 -2 -4 +2 -4 +2 +4 +8 Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao Electric Fence Stops your program on the exact instruction that overruns (or underruns) a malloc() memory buffer. Cooperate with gdb. Another tool: Dmalloc Log http://dmalloc.com/ Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao Thinking… How to detect a circular linking list? Csc352-Summer03, Stanley Yao

Csc352-Summer03, Stanley Yao Acknowledgement John H. Hartman, Classnotes for Csc352-Spring03, CS Dept., University of Arizona, 2003 Craig Chase, Dynamic Memory Management (ppt), The University of Texas at Austin, 2002 Brian W. Kernighan, Dennis M. Ritchie, The C Programming Language (2nd Ed.), Prentice Hall, 1988 Csc352-Summer03, Stanley Yao