Memory Allocation. Alan L. Cox

Similar documents
Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

Chapter 5 Names, Bindings, Type Checking, and Scopes

1 Abstract Data Types Information Hiding

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Leak Check Version 2.1 for Linux TM

The C Programming Language course syllabus associate level

Sources: On the Web: Slides will be available on:

Semantic Analysis: Types and Type Checking

Stacks. Linear data structures

Object Oriented Software Design II

C++ Programming Language

Lecture Data Types and Types of a Language

Lecture 22: C Programming 4 Embedded Systems

Illustration 1: Diagram of program function and data flow

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

ECS 165B: Database System Implementa6on Lecture 2

Introduction to Data Structures

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

Example 1/24. Example

Simple C Programs. Goals for this Lecture. Help you learn about:

Module 816. File Management in C. M. Campbell 1993 Deakin University

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Jonathan Worthington Scarborough Linux User Group

Moving from CS 61A Scheme to CS 61B Java

Pitfalls in Embedded Software

Variable Base Interface

10 Things Every Linux Programmer Should Know Linux Misconceptions in 30 Minutes

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Numerical Algorithms Group

C# and Other Languages

A C Test: The 0x10 Best Questions for Would-be Embedded Programmers

CSC230 Getting Starting in C. Tyler Bletsch

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Programming Embedded Systems

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

5 Arrays and Pointers

Programming languages C

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Building Embedded Systems

C++ INTERVIEW QUESTIONS

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

Java Interview Questions and Answers

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++FA 3.1 OPTIMIZING C++

The programming language C. sws1 1

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

6.S096 Lecture 1 Introduction to C

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

C++FA 5.1 PRACTICE MID-TERM EXAM

How To Write Portable Programs In C

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Common Data Structures

Static Code Analysis Procedures in the Development Cycle

/* File: blkcopy.c. size_t n

A Memory Model for Static Analysis of C Programs

System Calls Related to File Manipulation

1 The Java Virtual Machine

C Interview Questions

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Class Notes for CSCI 104: Data Structures and Object-Oriented Design

System Software Prof. Dr. H. Mössenböck

CS106A, Stanford Handout #38. Strings and Chars

Object Oriented Software Design II

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Client-server Sockets

Crash Course in Java

Node-Based Structures Linked Lists: Implementation

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Informatica e Sistemi in Tempo Reale

Explain the relationship between a class and an object. Which is general and which is specific?

7.1 Our Current Model

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Lecture 12 Doubly Linked Lists (with Recursion)

Data Structure with C

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

An Incomplete C++ Primer. University of Wyoming MA 5310

Glossary of Object Oriented Terms

CISC 181 Project 3 Designing Classes for Bank Accounts

Object Oriented Software Design II

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

CmpSci 187: Programming with Data Structures Spring 2015

Lecture 11: Tail Recursion; Continuations

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

MISRA-C:2012 Standards Model Summary for C / C++

DATA STRUCTURES USING C

Transcription:

Memory Allocation Alan L. Cox alc@rice.edu

Objectives Be able to recognize the differences between static and dynamic memory allocation Be able to use malloc() and free() to manage dynamic memory in your programs Be able to analyze programs for memory management related bugs Cox / Fagan Memory Allocation 2

Big Picture C gives you access to underlying data representations & layout Needed for systems programming Dangerous for application programming Necessary to understand Memory is a finite sequence of fixed-size storage cells Most machines view storage cells as bytes byte-addresses Individual bits are not addressable May also view storage cells as words Cox / Fagan Memory Allocation 3

A Running Program s Memory 0x7FFFFFFFFFFF Unused User Stack Created at runtime 47 bits of address space Shared Libraries Heap Read/Write Data Read-only Code and Data Shared among processes Created at runtime Loaded from the executable 0x000000000000 Unused Cox / Fagan Memory Allocation 4

Allocation For all data, memory must be allocated Allocated = memory space reserved Two questions: When do we know the size to allocate? When do we allocate? Two possible answers for each: Compile-time (static) Run-time (dynamic) Cox / Fagan Memory Allocation 5

How much memory to allocate? Sometimes obvious: Sometimes not: char c; int array[10]; char *c; int *array; One byte 10 * sizeof(int) (= 40 on CLEAR) Is this going to point to one character or a string? How big will this array be? How will these be used??? Will they point to already allocated memory (what we ve seen so far)? Will new memory need to be allocated (we haven t seen this yet)? Cox / Fagan Memory Allocation 6

malloc() Won t continually remind you of this #include <stdlib.h> int *array = malloc(num_items * sizeof(int)); Allocate memory dynamically Pass a size (number of bytes to allocate) Finds unused memory that is large enough to hold the specified number of bytes and reserves it Returns a void * that points to the allocated memory No typecast is needed for pointer assignment Essentially equivalent to new in Java and C++ Cox / Fagan Memory Allocation 7

Using malloc() int *i; int *array; Statically allocates space for 2 pointers i = malloc(sizeof(int)); array = malloc(num_items * sizeof(int)); Dynamically allocates space for data *i = 3; array[3] = 5; i and array are interchangeable Arrays pointers to the initial (0th) array element i could point to an array, as well May change over the course of the program Allocated memory is not initialized! calloc zeroes allocated memory (otherwise, same as malloc; details to come in lab) Cox / Fagan Memory Allocation 8

Using malloc() Always check the return value of system calls like malloc() for errors int *a = malloc(num_items * sizeof(int)); if (a == NULL) fprintf(stderr, Out of memory.\n ); exit(1); Terminate now! And, indicate error. For brevity, won t in class Lab examples will Textbook uses capitalization convention Capitalized version of functions are wrappers that check for errors and exit if they occur (i.e. Malloc) May not be appropriate to always exit on a malloc error, though, as you may be able to recover memory Cox / Fagan Memory Allocation 9

When to Allocate? Static time Typically global variables: int value; int main(void) Dynamic time Typically local variables: int f( ) int value; int main(void) Only one copy ever exists, so can allocate at compile-time One copy exists for each call may be unbounded # of calls, so can t allocate at compile-time Cox / Fagan Memory Allocation 10

When to Allocate? Static time Some local variables: One copy exists for all calls allocated at compile-time int f( ) static int value; int main(void) Confusingly, local static has nothing to do with global static! Cox / Fagan Memory Allocation 11

Allocation in Process Memory 0x7FFFFFFFFFFF Stack Static size, dynamic allocation Local variables Shared Libraries Programmer controlled (variable-sized objects) Heap Read/Write Data Read-only Code and Data Dynamic size, dynamic allocation Static size, static allocation Global variables (and static local variables) 0x000000000000 Cox / Fagan Memory Allocation 12

Deallocation Space allocated via declaration (entering scope) is deallocated when exiting scope f(void) int y; int array[10]; Can t refer to y or array outside of f(), so their space is deallocated upon return Cox / Fagan Memory Allocation 13

Deallocation malloc() allocates memory explicitly Must also deallocate it explicitly (using free())! Not automatically deallocated (garbage collected) as in Python and Java Forgetting to deallocate leads to memory leaks & running out of memory int *a = malloc(num_items * sizeof(int)); free(a); a = malloc(2 * num_items * sizeof(int)); Must not use a freed pointer unless reassigned or reallocated Cox / Fagan Memory Allocation 14

Deallocation Space allocated by malloc() is freed when the program terminates If data structure is used until program termination, don t need to free Entire process memory is deallocated Cox / Fagan Memory Allocation 15

Back to create_date Date * create_date3(int month, int day, int year) Date *d; Date * create_date3(int month, int day, int year) Date *d; d->month = month; d->day = day; d->year = year; return (d); d = malloc(sizeof(date)); d->month = month; d->day = day; d->year = year; return (d); Cox / Fagan Memory Allocation 16

Pitfall void foo(void) Date *today; today = create_date3(1, 24, 2017); Potential problem: memory allocation is performed in this function (may not know its implementation) /* use today */... return; Memory is still allocated for today! Will never be deallocated (calling function doesn t even know about it) Cox / Fagan Memory Allocation 17

Possible Solutions void foo(void) Date *today; void foo(void) Date *today; today = create_date3( ); today = create_date3( ); /* use today */... /* use today */... free(today); return; destroy_date(today); return; Explicitly deallocate memory specification of create_date3 must tell you to do this Complete the abstraction create has a corresponding destroy Cox / Fagan Memory Allocation 18

Common Memory Management Mistakes Cox / Fagan Memory Allocation 19

What s Wrong With This Code? int *f( ) int i; return (&i); int *make_array( ) int array[10]; return (array); Consider j = *f() Leads to referencing deallocated memory Never return a pointer to a local variable! Behavior depends on allocation pattern Space not reallocated (unlikely) works Space reallocated very unpredictable Cox / Fagan Memory Allocation 20

One Solution int *f( ) int *i_ptr = malloc(sizeof(int)); return (i_ptr); int *make_array( ) int *array = malloc(10 * sizeof(int)); return (array); Allocate with malloc(), and return the pointer Upon return, space for local pointer variable is deallocated But the malloc-ed space isn t deallocated until it is free-d Potential memory leak if caller is not careful, as with create_date3 Cox / Fagan Memory Allocation 21

What s Wrong With This Code? Initializatio n loop for i=0 y[] j=0 /* return y = Ax */ int *matvec(int **A, int *x) int *y = malloc(n * sizeof(int)); int i, j; for (; i<n; i+=1) for (; j<n; j+=1) y[i] += A[i][j] * x[j]; return (y); malloc-ed & declared space is not initialized! i, j, y[i] initially contain unknown data garbage Often has zero value, leading to seemingly correct results Cox / Fagan Memory Allocation 22

What s Wrong With This Code? char **p; int i; /* Allocate space for M*N matrix */ p = malloc(m * sizeof(char)); char * for (i = 0; i < M; i++) p[i] = malloc(n * sizeof(char)); Allocates wrong amount of memory Leads to writing unallocated memory Cox / Fagan Memory Allocation 23

Explanation Heap region in memory (each rectangle represents one byte) Assume M = N = 2, a memory address is 64 bit or 8 byte ` for (i = 0; i < M; i++) p[i] = malloc(n * sizeof(char)); p[0] p = malloc(m * sizeof(char)); Cox / Fagan Memory Allocation 24

Corrected code Heap region in memory (each rectangle represents one byte) Assume M = N = 2, a memory address is 64 bit or 8 byte for (i = 0; i < M; i++) p[i] = malloc(n * sizeof(char)); p[1] ` p[0] p = malloc(m * sizeof(char *)); Cox / Fagan Memory Allocation 25

What s Wrong With This Code? char **p; int i; < /* Allocate space for M*N matrix */ p = malloc(m * sizeof(char *)); for (i=0; i<=m; i+=1) p[i] = malloc(n * sizeof(char)); Off-by-1 error Uses interval 0 M instead of 0 M-1 Leads to writing unallocated memory Be careful with loop bounds! Cox / Fagan Memory Allocation 26

Using const const int * size Pointer to a constant integer Cannot write to *size int * const size Constant pointer to an integer Cannot modify the pointer (size) Can write to *size char * xyz(char * to, const char * from) char *save = to; for (; (*to = *from); ++from, ++to); return(save); Cox / Fagan Memory Allocation 27

What s Wrong With This Code? char *s = 1234567 ; char t[7]; strcpy(t, s); char * strcpy(char * to, const char * from) char *save = to; for (; (*to = *from); ++from, ++to); return(save); t[] doesn t have space for string terminator Leads to writing unallocated memory One way to avoid: char *s = 1234567 ; char *t = malloc((strlen(s) + 1) * sizeof(char)); strcpy(t, s); Cox / Fagan Memory Allocation 28

What s Wrong With This Code? /* Search memory for a value. */ /* Assume value is present. */ int *search(int *p, int value) while (*p > 0 && *p!= value) p += sizeof(int); p += 1; return (p); Misused pointer arithmetic Search skips some data, can read unallocated memory, and might not ever see value Should never add sizeof() to a pointer Could consider rewriting this function & its uses to use array notation instead Cox / Fagan Memory Allocation 29

What s Wrong With This Code? x = malloc(n * sizeof(int)); free(x); y = malloc(m * sizeof(int)); for (i = 0; i < M; i++) y[i] = x[i]; x[i] += 1; Premature free() Reads and writes deallocated memory Behavior depends on allocation pattern Space not reallocated works Space reallocated very unpredictable Cox / Fagan Memory Allocation 30

What s Wrong With This Code? free(x); void foo(void) int *x = malloc(n * sizeof(int)); return; Memory leak doesn t free malloc-ed space Data still allocated, but inaccessible, since can t refer to x Slows future memory performance Cox / Fagan Memory Allocation 31

What s Wrong With This Code? struct ACons int first; struct ACons *rest; ; typedef struct ACons *List; A peek at one way to define lists List cons(int first, List rest) List item = malloc(sizeof(struct ACons)); item->first = first; item->rest = rest; return (item); void foo(void) List list = cons(1, cons(2, cons(3, NULL))); free(list); return; Cox / Fagan Memory Allocation 32

Example continued Memory leak frees only beginning of data structure Remainder of data structure is still allocated, but inaccessible Need to write deallocation (destructor) routines for each data structure Cox / Fagan Memory Allocation 33

Putting it together string pointer struct malloc() simple I/O simple string operations Cox / Fagan Memory Allocation 34

What does action1() do? struct thing char *stuff; #include <stdio.h> struct thing *another_thing; ; #include <stdlib.h> void action1(struct thing **y, const char *stuff) #include <string.h> struct thing *x = malloc(sizeof(struct thing)); action1() inserts a new node storing the specified /* Alternatively, x->stuff = strdup(stuff); */ string into the linked list x->stuff = malloc(strlen(stuff) + 1); strcpy(x->stuff, stuff); Cox / Fagan Memory Allocation 35

What does action2() do? struct thing char *stuff; struct thing *another_thing; ; void action2(struct thing **y) struct thing *x; action2() prints the strings stored in the linked list nodes sequentially while ((x = *y)!= NULL) printf("%s ", x->stuff); y = &x->another_thing; Cox / Fagan Memory Allocation 36

What does action3() do? struct thing char *stuff; struct thing *another_thing; ; int action3(struct thing **y, const char *stuff) struct thing *x; while ((x = *y)!= NULL) if (strcmp(x->stuff, stuff) == 0) else action3() finds out whether a string is stored in the linked list return (1); Cox / Fagan y = &x->another_thing; Memory Allocation 37

What does action4() do? struct thing char *stuff; struct thing *another_thing; ; void action4(struct thing **y, const char *stuff) struct thing *x; while ((x = *y)!= NULL) if (strcmp(x->stuff, stuff) == 0) style compromised to save space action4() deletes the first list node that stores the specified string *y = x->another_thing; Cox / Fagan Memory Allocation 38

Next Time Lab: Debugging Assembly Cox / Fagan Memory Allocation 39