CS 0449 Jarrett Billingsley

Slides:



Advertisements
Similar presentations
Dynamic Memory Management
Advertisements

Lecture 10: Heap Management CS 540 GMU Spring 2009.
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:
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
CS 1114: Data Structures – memory allocation Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
C and Data Structures Baojian Hua
Linked lists and memory allocation Prof. Noah Snavely CS1114
CS61C Midterm Review on C & Memory Management Fall 2006 Aaron Staley Some material taken from slides by: Michael Le Navtej Sadhal.
Memory Layout C and Data Structures Baojian Hua
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.
Stack and Heap Memory Stack resident variables include:
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of daily. If you me and.
Memory Management What if pgm mem > main mem ?. Memory Management What if pgm mem > main mem ? Overlays – program controlled.
Memory – Virtual Memory, Virtual Machines
CSE 220 – C Programming malloc, calloc, realloc.
Memory Management.
Dynamic Allocation in C
CS/COE 0449 (term 2174) Jarrett Billingsley
Dynamic Storage Allocation
Computer Organization and Design Pointers, Arrays and Strings in C
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
Protecting Memory What is there to protect in memory?
CS/COE 0447 (term 2181) Jarrett Billingsley
Overview 4 major memory segments Key differences from Java stack
CSE 374 Programming Concepts & Tools
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Lesson One – Creating a thread
Programs – Loading and Running an Executable
ENEE150 Discussion 07 Section 0101 Adam Wang.
Storage Management.
CS 153: Concepts of Compiler Design November 28 Class Meeting
Programmazione I a.a. 2017/2018.
Checking Memory Management
CSCI206 - Computer Organization & Programming
Dynamic Memory CSCE 121 J. Michael Moore.
Pointers and Dynamic Variables
Dynamically Allocated Memory
Circular Buffers, Linked Lists
Storage.
C – Scope, Lifetime, and the Stack
CS/COE 0449 Jarrett Billingsley
Overview 4 major memory segments Key differences from Java stack
Dynamic Memory A whole heap of fun….
Dynamic Memory Allocation
Memory Allocation CS 217.
Given the code to the left:
CS/COE 0449 Jarrett Billingsley
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
OS – Memory Deallocation
Introduction to C++ Linear Linked Lists
Arrays an array of 5 ints is filled with 3,2,4,1,7
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Dynamic Memory CSCE 121.
Run-time environments
Week 7 - Friday CS222.
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
CS/COE 0449 Jarrett Billingsley
Presentation transcript:

CS 0449 Jarrett Billingsley C – Memory management CS 0449 Jarrett Billingsley

Class announcements hmmmmmmmm nah nothing CS449

The Heap CS449

Two new worlds local variables are allocated… where, again? what is their lifetime (from allocation to deallocation)? global variables are put in the data segment this area is automatically allocated by the OS its lifetime lasts for your entire program there is also a read-only data segment for constants but the third place is new…ish new like… new Object() kinda new - locals are allocated on the stack (within activation records), and their lifetime is the body of the function CS449

The heap the heap is a programmer-managed area of memory it has nothing to do with the heap data structure you can create and destroy pieces of memory on demand this is a powerful ability! but remember… WITH GREAT POWER COMES GREAT RESPONSIBILITY… UNCLE BEN NOOOO FREE EVERYTHING YOU MALLOC EXACTLY ONCE HUH?? CS449

Trainwreck Stack Heap the stack and heap both change size the stack grows down (higher addresses to lower) the heap grows up (lower addresses to higher) if they meet, the program is out of memory Why? why not have the stack grow up and the heap down? why not have them grow outward from the center? why not have them both grow up or down? why have them in the same area of memory at all? 64-bit architectures win here! in 16- or 32-bit address spaces, we don't have a choice 0xFFFF Stack Heap - having the stack and heap grow towards each other allows for maximum memory utilization, e.g. if you have a huge heap and small stack - the stack could grow up but for whatever reason we've settled on the stack growing down in most architectures - in 64-bit architectures, the stack and heap addresses can be exabytes apart and therefore will never, ever collide 0x0000 CS449

The functions int* arr = malloc(sizeof(int) * 20); free(arr); the heap functions are in <stdlib.h> to allocate memory, you use malloc: int* arr = malloc(sizeof(int) * 20); this allocates a 20-item int array on the heap malloc takes the number of bytes to allocate, makes a block of bytes that big, and returns a pointer to it when you're done with that block of memory, you use free: free(arr); - seems simple, but it ain't. CS449

Allocating a struct on the heap this is the closest to Java's new as C gets: Food* f = malloc(sizeof(Food)); but just like anything on the stack… everything you malloc contains garbage to fix this you can use memset from <string.h> memset(f, 0, sizeof(Food)); this says "fill the memory at f with 0s" orrrr use calloc() instead of malloc() Food* f = calloc(sizeof(Food), 1); this says "allocate 1 * sizeof(Food) bytes and fill them with 0s" - it's garbage for the same reason the stack contains garbage: it's old, dirty, used memory EWWWW - I like calloc. I like what it does and I like how it sounds. calloc calloc calloc. CS449

The caveats while(1) malloc(1048576); if you do this: your program will run out of memory but you won't get an error this will loop forever because… if you run out of memory, MALLOC RETURNS NULL - if you watch the memory usage, it'll go up and up and up and then stop going up and then sit there using 100% CPU. - this is a shitty program, don't run it. CS449

The caveats int* arr = malloc(sizeof(int) * 20); free(arr); and what's worse: int* arr = malloc(sizeof(int) * 20); free(arr); arr[0] = 1000; // hmmm? who knows what'll happen! when you free heap memory, all pointers to it become invalid, and it's your responsibility to never use them again so if multiple things point to this memory... well, they're allowed to point to it, but they better not access it. it's like a bomb rigged to explode when you dereference it. - here's another kind of invalid pointer: it used to point to some valid heap memory, but not anymore. CS449

The Heap Commandments When you malloc, you should check if it returns NULL. You must free everything you allocate. …and you must free it exactly once. You must not access memory that has been freed. these are the really tricky ones how do you know how many things are using a piece of memory? …can you guarantee that? who is responsible for deallocating? (who's the owner?) what if ten things point to it? what if… WHAT IF NOBODY POINTS TO IT OH NO CS449

What’s Garbage Collection? CS449

Messy, messy (animated) let's say you made a linked list Stack head Heap NULL 30 20 10 null what would happen if we did this? head = NULL; what about our poor linked list?  - well nobody is pointing to that list anymore. - where does it go…? NOWHERE. CS449

Dynamic memory management is hard. don't believe anyone who says otherwise but mathematically, it’s not a very hard problem if only we had some kind of automatic, programmable machine that could do it for us  ………………………………………………… …………………………………………………………………………………………………………………………………………………………………………………………………… oh wait! - WE DO!!!!!!! CS449

an object is reachable if there is a path from the roots to the object A touching story there are two useful concepts from GC: roots and reachability Heap an object is reachable if there is a path from the roots to the object Roots are any part of memory that isn’t the heap Stack Global Variables - GC = garbage collection CS449

Pruning time (animated) If we remove the only link to an object… Heap It’s no longer reachable. It’s garbage. A garbage collector comes and picks it up. Roots Stack Global Variables - we can cause a whole graph of objects to become unreachable by removing only one link. CS449

Nobody will come pick it up. Get a rake (animated) C, on the other hand, is stupid. If we cut the last link: Heap Nobody will come pick it up. If we keep doing this… Roots Stack Global Variables CS449

This is a MEMORY LEAK. if heap memory becomes unreachable in C, it’s a memory leak. we can never deallocate it. it's like it "leaked out" of the computer. the only way to deallocate it is to exit your program. all your program's memory is deallocated when you exit. decades of research have gone into making GC useful! never take it for granted! well, now that you’re using C, you’ll learn not to… - there are other "flavors" of memory leaks - "a cache without a removal policy is just a memory leak in disguise" CS449

VLAs and alloca() CS449

Variable-size convenience; automatic deletion! modern C code has access to two ways to allocate variable sized pieces of memory on the stack, instead of the heap you could try this: int len = strlen(str); char newArray[len]; // not a constant size! this is a C99 feature, called variable-length arrays (VLAs) C89 only allows array variables to have constant sizes VLAs let you specify the size at runtime it's still on the stack! which gives you automatic lifetime! CS449

alloca() int* arr = alloca(sizeof(int) * 20); the more general-purpose way to do this is with alloca it looks just like malloc: int* arr = alloca(sizeof(int) * 20); just like VLAs, this allocates memory on the stack just like VLAs, this memory has automatic lifetime so should you return this arr pointer? ;o well these are great! why don't we use them all the time?! CS449

Well you can, but watch out on most systems, stack space is limited. on unix systems like thoth, ulimit -s tells you the limit on thoth, it's only 10 MB both VLAs and alloca have to obey this limit this doesn't solve the problem of "ownership." in cases where it's obvious that this function owns the memory, it works just fine. but if you need to hand this pointer off to someone else… you're asking for trouble. also, alloca is not portable not all OSes/architectures support it! - most "big" computers support alloca(), mostly it's tiny embedded ones that don't CS449