o Code, executable, and process o Main memory vs. virtual memory

Similar documents
CSC 1600 Memory Layout for Unix Processes"

In Java we have the keyword null, which is the value of an uninitialized reference type

Memory Management. CSC215 Lecture

Lecture 8 Dynamic Memory Allocation

Memory (Stack and Heap)

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

Intermediate Programming, Spring 2017*

Class Information ANNOUCEMENTS

Dynamic Memory Management

Memory Management. CS449 Fall 2017

Dynamic Memory Management

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

C Structures & Dynamic Memory Management

COSC Software Engineering. Lecture 16: Managing Memory Managers

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic Memory Management! Goals of this Lecture!

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

14. Memory API. Operating System: Three Easy Pieces

Limitations of the stack

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Processes. Johan Montelius KTH

Hacking in C. Memory layout. Radboud University, Nijmegen, The Netherlands. Spring 2018

A process. the stack

Reminder: compiling & linking

Arrays and Memory Management

Lecture 14. Dynamic Memory Allocation

Memory and C/C++ modules

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Dynamic memory allocation (malloc)

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Dynamic Memory. R. Inkulu (Dynamic Memory) 1 / 19

Programs in memory. The layout of memory is roughly:

Dynamic Memory Allocation I

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Dynamic Memory Allocation I Nov 5, 2002

CS 222: Pointers and Manual Memory Management

Last week. Data on the stack is allocated automatically when we do a function call, and removed when we return

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Dynamic Allocation of Memory

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Memory Allocation and Linked Lists

Dynamic Allocation in C

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Dynamic Allocation in C

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

CS61C : Machine Structures

Understanding Pointers

TI2725-C, C programming lab, course

DAY 3. CS3600, Northeastern University. Alan Mislove

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

ME964 High Performance Computing for Engineering Applications

Dynamic memory allocation

CS 33. Intro to Storage Allocation. CS33 Intro to Computer Systems XXVI 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Memory Allocation. General Questions

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

ECE551 Midterm Version 1

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Cpt S 122 Data Structures. Data Structures

Dynamic Memory Allocation

ECE551 Midterm Version 1

Heap Arrays. Steven R. Bagley

Dynamic Memory Allocation

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

CS 11 C track: lecture 5

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

The C++ Object Lifecycle. EECS 211 Winter 2019

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Dynamic Memory Allocation. Zhaoguo Wang

High Performance Computing Lecture 1. Matthew Jacob Indian Institute of Science

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

Content. In this chapter, you will learn:

Pointers and Memory Management

Introduction to Linked List: Review. Source:

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

ECE 551D Spring 2018 Midterm Exam

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

ANITA S SUPER AWESOME RECITATION SLIDES

MPATE-GE 2618: C Programming for Music Technology. Unit 5.1

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Memory and C/C++ modules

CS61C : Machine Structures

Dynamic Allocation of Memory

Compilation/linking revisited. Memory and C/C++ modules. Layout of C/C++ programs. A sample C program demo.c. A possible structure of demo.

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

ch = argv[i][++j]; /* why does ++j but j++ does not? */

Dynamic Memory Allocation: Basic Concepts

ECE551 Midterm Version 2

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

CS 241 Data Organization Binary Trees

Contents of Lecture 3

Secure Software Programming and Vulnerability Analysis

CMSC 330: Organization of Programming Languages

Transcription:

Goals for Today s Lecture Memory Allocation Prof. David August COS 217 Behind the scenes of running a program o Code, executable, and process o Main memory vs. virtual memory Memory layout for UNIX processes, and relationship to C o : code and constant data o : initialized global and static variables o : uninitialized global and static variables o : dynamic memory o : local variables C functions for memory management o malloc: allocate memory from the heap o free: deallocate memory from the heap 1 2 Code vs. Executable vs. Process Main Memory on a Computer C source code o C statements organized into functions o Stored as a collection of files (.c and.h) Executable module o Binary image generated by compiler o Stored as a file (e.g., a.out) Process o Instance of a program that is executing With its own address space in memory With its own id and execution state o Managed by the operating system C source code compiling executable running process What is main memory? o Storage for variables, data, code, etc. o May be shared among many processes CPU Memory Network Bus Disk Audio Video 3 4

Virtual Memory for a Process What to Store: Code and Constants What is virtual memory? o Contiguous addressable memory space for a single process o May be swapped into physical memory from disk in pages o Let s you pretend each process has its own contiguous memory Network Audio Executable code and constant data o Program binary, and any shared libraries it loads o Necessary for OS to read the commands OS knows everything in advance o Knows amount of space needed o Knows the contents of the memory CPU 32 32 Memory Bus Disk Video Virtual Address Space Known as the text segment Note: Some systems (e.g. hats) store some constants in rodata section 5 6 What to Store: Static What to Store: Dynamic Memory Variables that exist for the entire program o Global variables, and static local variables o Amount of space required is known in advance Memory allocated while program is running o E.g., allocated using the malloc() function And deallocated using the free() function : initialized in the code o Initial value specified by the programmer E.g., int x = 97; o Memory is initialized with this value : not initialized in the code o Initial value not specified E.g., int x; o All memory initialized to (on most OS s) o stands for Block Started by Symbol OS knows nothing in advance o Doesn t know the amount of space o Doesn t know the contents So, need to allow room to grow o Known as the heap o Detailed example in a few slides o More in programming assignment #4 7 8

What to Store: Temporary Variables Memory Layout: Summary Temporary memory during lifetime of a function or block o Storage for function parameters and local variables Need to support nested function calls o One function calls another, and so on o Store the variables of calling function o Know where to return when done So, must allow room to grow o Known as the stack o Push on the stack as new function is called o Pop off the stack as the function ends : code, constant data : initialized global & static variables : uninitialized global & static variables : dynamic memory : local variables Detailed example later on 9 1 Memory Layout Example Memory Layout Example: 11 12

Memory Layout Example: Memory Layout Example: 13 14 Memory Layout Example: Memory Layout Example: 15 16

Memory Allocation & Deallocation Memory Allocation Example How, and when, is memory allocated? o Global and static variables = program startup o Local variables = function call o Dynamic memory = malloc() How is memory deallocated? o Global and static variables = program finish o Local variables = function return o Dynamic memory = free() All memory deallocated when program ends o It is good style to free allocated memory anyway : hello at startup : at startup : at function call : 8 bytes at malloc 17 18 Memory Deallocation Example Memory Initialization Available till termination Available till termination Deallocate on return from f Deallocate on free() Local variables have undefined values int count; Memory allocated by malloc() has undefined values char* p = (char *) malloc(8); If you need a variable to start with a particular value, use an explicit initializer int count = ; p[] = \ ; Global and static variables are initialized to by default 19 static int count = ; is the same as static int count; It is bad style to depend on this 2

: Dynamic Memory : Dynamic Memory p2 21 22 : Dynamic Memory : Dynamic Memory p2 p2 23 24

: Dynamic Memory : Dynamic Memory p2 p2 25 26 : Dynamic Memory : Dynamic Memory p5, p2 p5, p2 27 28

: Dynamic Memory : Dynamic Memory p5, p2 p5, p2 29 3 How Do Malloc and Free Work? Simple answer o Doesn t matter o Good modularity means you can use it without understanding it Real answer malloc(s) n = s / sizeof(int) 1 word of overhead n words of user data n free(p) put p into linked list of free objects n 31 Using Malloc and Free Types o void*: generic pointer to any type (can be converted to other types) o size_t: unsigned integer type returned by sizeof() void* malloc(size_t size) o Returns a pointer to space of size size o or NULL if the request cannot be satisfied o E.g., int* x = (int *) malloc(sizeof(int)); void* calloc(size_t nobj, size_t size) o Returns a pointer to space for array of nobj objects of size size o or NULL if the request cannot be satisfied o Bytes are initialized to void free(void* p) o Deallocate the space pointed to by the pointer p o Pointer p must be pointer to space previously allocated o Do nothing if p is NULL 32

Using realloc and (never) alloca void* realloc(void* ptr, size_t size) o Grows the allocated buffer o Moves/copies the data if old space insufficient o or NULL if the request cannot be satisfied void* alloca(size_t size) o Not guaranteed to exist (not in any official standard) o Allocates space on local stack frame o Space automatically freed when function exits o Particularly useful for following: int calc(int numitems) int items[numitems]; int *items = alloca(numitems * sizeof(int)); Sorting w/o Linked Lists int alloc = 4, used = ; Item *temp, *buf = NULL; while ((temp = NextItem())!= NULL) if (used >= alloc) alloc *= 2; buf = realloc(buf, alloc*sizeof(item)); buf[used++] = temp; qsort( ); 33 34 Avoid Leaking Memory Memory leaks lose references to dynamic memory int f(void) p = (char *) malloc(8 * sizeof(char)); return ; Avoid Dangling Pointers Dangling pointers point to data that s not there anymore char *f(void) char p[8]; int main(void) f(); 35 int main(void) char *res = f(); 36

Debugging Malloc Problems Symptom: random failures, especially on call return o Corrupted the stack frame return info Symptom: calls to malloc/free fail o Corrupted the malloc bookkeeping data Symptom: program magically works if printf inserted o Corrupted storage space in stack frame Debugging mallocs exist o Doing man malloc on Linux reveals MALLOC_CHECK_ o Searching debug malloc yields dmalloc, other libraries o Larger problems: valgrind, electric fence, etc. Summary Five types of memory for variables o : code, constant data (constant data in rodata on hats) o : initialized global & static variables o : uninitialized global & static variables o : dynamic memory o : local variables Important to understand differences between o Allocation: space allocated o Initialization: initial value, if any o Deallocation: space reclaimed Understanding memory allocation is important o Make efficient use of memory o Avoid memory leaks from dangling pointers 37 38