Chien-Chung Shen CIS/UD

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.
Chapter Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
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:
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
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’;
Introduction to C Programming CE Lecture 18 Dynamic Memory Allocation and Ragged Arrays.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
C and Data Structures Baojian Hua
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Memory Layout C and Data Structures Baojian Hua
Outline Midterm results Static variables Memory model
CS50 SECTION: WEEK 4 Kenny Yu. Announcements  Problem Set 4 Walkthrough online  Problem Set 2 Feedback has been sent out  CORRECTION: Expect all future.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
February 11, 2005 More Pointers Dynamic Memory Allocation.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Pointers and Arrays Beyond Chapter Pointers and Arrays What are the real differences? Pointer Holds the address of a variable Can be pointed.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
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.
Chapter 14 Memory API Chien-Chung Shen CIS, UD
Prachi A. Joshi Assistant Professor in CSE DIEMS,Aurangabad Unit 1 : Basic Concepts Pointers and dynamic memory allocation, Algorithm Specification, Data.
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.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
One-dimensional arrays and strings: Chapter 6, Slide 1 The concept of array - an extension of the basic model of memory:
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSE 220 – C Programming malloc, calloc, realloc.
Chapter 17 Free-Space Management
Stack and Heap Memory Stack resident variables include:
Computer Organization and Design Pointers, Arrays and Strings in C
Day 03 Introduction to C.
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
Chien-Chung Shen CIS/UD
Day 03 Introduction to C.
Checking Memory Management
CSCI206 - Computer Organization & Programming
The Hardware/Software Interface CSE351 Winter 2013
Dynamic Memory Allocation
Dynamic Memory Allocation
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
CSCI206 - Computer Organization & Programming
CSC 253 Lecture 8.
CSC 253 Lecture 8.
Dynamic Memory Allocation
Memory Allocation CS 217.
Outline Defining and using Pointers Operations on pointers
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
CSC215 Homework Homework 06 Due date: Oct 30, 2016.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Pointers and Arrays Beyond Chapter 16
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
CISC 361 Operating Systems Midterm Review
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Chien-Chung Shen CIS/UD cshen@udel.edu Chapter 14 Memory API Chien-Chung Shen CIS/UD cshen@udel.edu

Crux: How to Allocate and Manage Memory In Unix/C programs, understanding how to allocate and manage memory is critical in building robust and reliable software

Types of Memory In running a C program, two types of memory are allocated Stack (or automatic) memory – managed implicitly by compiler for programmer (allocation/deallocation) void func() { int x; // declares an integer on the stack ... } Heap memory – explicitly managed by programmer int *p = (int *) malloc(sizeof(int)); free(p); The may cause of many bugs (segmentation faults)

malloc() int *x = malloc(10 * sizeof(int)); printf("%d\n", sizeof(x)); int y[10]; printf("%d\n", sizeof(y)); sizeof() is a compile-time operator (i.e., the actual size is known at compile time) [so that it is not a function call, as a function call would take place at run-time] How big a pointer to an integer is (4 or 8); not how much memory was dynamically allocated There is enough static information for compiler to know that 40 bytes have been allocated

malloc() Returns [a pointer to type void] (i.e., void *) Cast the return type of malloc() to [a pointer to a specific type] int *x = (int *) malloc(10 * sizeof(int)); Casting doesn’t really accomplish anything, other than tell the compiler and other programmers who might be reading your code: “yeah, I know what I’m doing.” By casting the result of malloc(), the programmer is just giving some reassurance; the cast is not needed for the correctness

free() void func() { int *p = (int *) malloc(sizeof(int)); ... free(p); } The size of the allocated region is not passed in by the user, and must be tracked by the memory-allocation library itself

Memory Bugs (Character) strings!!! Forget to allocate memory char *src = "hello”; char *dst; // oops! unallocated strcpy(dst, src); // segfault and die Not allocate enough memory char *src = "hello"; char *dst = (char *) malloc(strlen(src)); strcpy(dst, src); Use malloc(strlen(src) + 1) for extra end-of-string character Forget to initialize allocated memory Forget to free memory – memory leak // too small! // buffer overflow