Chapter 14 Memory API Chien-Chung Shen CIS, UD

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
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.
Buffer Overflow Prabhaker Mateti Wright State University.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
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.
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.
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
1 Memory Allocation Professor Jennifer Rexford COS 217.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Dynamic Memory Management CAS CS210 Ying Ye Boston University.
CIS 415 – Operating System (Lab) CIS 415 Lab 5 Valgrind (memcheck) Dave Tian.
ספטמבר 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:
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Stack buffer overflow
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
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
Computer Science 210 Computer Organization Pointers and Dynamic Storage.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Outline Midterm results Static variables Memory model
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.
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:
David Notkin Autumn 2009 CSE303 Lecture 12 October 24, 2009: Space Needle.
Storage Bindings Allocation is the process by which the memory cell or collection of memory cells is assigned to a variable. These cells are taken from.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
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.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
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.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Stack and Heap Memory Stack resident variables include:
Computer Science 210 Computer Organization
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
2016.
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Dynamic Memory Allocation
Some examples.
CS157: Dynamic Memory Dynamic Memory 11/9/201804/25/06.
Storage.
Computer Science 210 Computer Organization
Dynamic Memory Allocation
Memory Allocation CS 217.
CSC215 Homework Homework 06 Due date: Oct 30, 2016.
Chien-Chung Shen CIS/UD
Pointers and Arrays Beyond Chapter 16
7. Pointers, Dynamic Memory
C Programming Lecture-8 Pointers and Memory Management
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
How Memory Leaks Work with Memory Diagram
Presentation transcript:

Chapter 14 Memory API Chien-Chung Shen CIS, UD

Types of Memory Stack (or automatic) memory – managed implicitly by compiler for programmer void func() { int x; // declares an integer on the stack... } Heap memory – explicitly managed by programmer void func() { int *x = (int *) malloc(sizeof(int));... }

Size of Memory Allocation int *x = malloc(10 * sizeof(int)); printf("%d\n", sizeof(x)); int y[10]; printf("%d\n", sizeof(y)); there is enough static information for compiler to know that 40 bytes have been allocated sizeof() is a compile-time operator

Memory Bugs Forget to allo cate 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)); // too small! strcpy(dst, src); // work properly, but buffer overflow Forget to initialize allocated memory Forget to free memory – memory leak