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

Similar documents
Dynamic Memory Allocation: Advanced Concepts

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Dynamic Memory Allocation

Dynamic Memory Allocation II October 22, 2008

Dynamic Memory Allocation

Today. Dynamic Memory Allocation: Advanced Concepts. Explicit Free Lists. Keeping Track of Free Blocks. Allocating From Explicit Free Lists

Dynamic Memory Alloca/on: Advanced Concepts

NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Dynamic Memory Allocation

Dynamic Memory Allocation: Advanced Concepts

Memory Allocation III

Dynamic Memory Allocation: Advanced Concepts

Memory Allocation III CSE 351 Spring (original source unknown)

Memory Allocation III

Internal Fragmentation

Memory Allocation III

Foundations of Computer Systems

Memory Management. CSC215 Lecture

Lecture 8 Dynamic Memory Allocation

Memory Allocation III

Memory (Stack and Heap)

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

Dynamic Memory Alloca/on: Advanced Concepts

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

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

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

Programming in C. Lecture 5: Aliasing, Graphs, and Deallocation. Dr Neel Krishnaswami. Michaelmas Term University of Cambridge

CS24 Week 2 Lecture 1

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

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

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

14. Memory API. Operating System: Three Easy Pieces

Dynamic memory. EECS 211 Winter 2019

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

DAY 3. CS3600, Northeastern University. Alan Mislove

Heap Arrays. Steven R. Bagley

Dynamic memory allocation

Dynamically Allocated Memory in C

Agenda. Dynamic Memory Management. Robert C. Seacord. Secure Coding in C and C++

Class Information ANNOUCEMENTS

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

Fall 2018 Discussion 2: September 3, 2018

Secure Coding in C and C++

Secure Coding in C and C++ Dynamic Memory Management Lecture 5 Jan 29, 2013

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Dynamic Allocation of Memory

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

CS61, Fall 2012 Section 2 Notes

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

Dynamic Data Structures. CSCI 112: Programming in C

Exercise Session 3 Systems Programming and Computer Architecture

C Structures & Dynamic Memory Management

CSE2301. Dynamic memory Allocation. malloc() Dynamic Memory Allocation and Structs

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

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

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CS 11 C track: lecture 5

CS61 Section Notes. Section 5 (Fall 2011) Topics to be covered Common Memory Errors Dynamic Memory Allocation Assignment 3: Malloc

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

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

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

Dynamic Memory Allocation and Linked Lists

C Programming Basics II

Dynamic Memory Management

CS 61C: Great Ideas in Computer Architecture Introduction to C, Part III

Memory Allocation. General Questions

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

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Chapter 19 Data Structures -struct -dynamic memory allocation

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

CS 61C: Great Ideas in Computer Architecture. Memory Management and Usage

Limitations of the stack

Arrays and Memory Management

Recitation #11 Malloc Lab. November 7th, 2017

CS 110 Computer Architecture. Lecture 4: Introduction to C, Part III. Instructor: Sören Schwertfeger.

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

Secure Software Programming and Vulnerability Analysis

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

Systems Programming and Computer Architecture ( )

CS 222: Pointers and Manual Memory Management

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Understanding Pointers

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

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

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CSC C69: OPERATING SYSTEMS

Cpt S 122 Data Structures. Data Structures

Pointers, Arrays, Memory: AKA the cause of those Segfaults

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong


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

Data Representation and Storage

CSE 351 Final Exam - Winter 2017

Stacks and Queues. CSE Data Structures April 12, 2002

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

Transcription:

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

A Queue head typedef struct list_element { int value; struct list_element *next; list_element_t; 67 17 list_element_t *head, *tail; 2 tail 14 CS33 Intro to Computer Systems XXVI 2 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Enqueue int enqueue(int value) { list_element_t *newle = (list_element_t *)malloc(sizeof(list_element_t)); if (newle == 0) return 0; newle->value = value; newle->next = 0; if (head == 0) { // list was empty assert(tail == 0); head = newle; else { tail->next = newle; tail = newle; return 1; CS33 Intro to Computer Systems XXVI 3 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dequeue int dequeue(int *value) { list_element_t *first; if (head == 0) { // list is empty return 0; *value = head->value; first = head; head = head->next; if (tail == first) { assert(head == 0); tail = 0; return 1; What s wrong with this code??? CS33 Intro to Computer Systems XXVI 4 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Storage Leaks int main() { while(1) if (malloc(sizeof(list_element_t)) == 0) break; return 1; For how long will this program run before terminating? CS33 Intro to Computer Systems XXVI 5 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dequeue, Fixed int dequeue(int *value) { list_element_t *first; if (head == 0) { // list is empty return 0; *value = head->value; first = head; head = head->next; if (tail == first) assert(head == 0); tail = 0; free(first); return 1; CS33 Intro to Computer Systems XXVI 6 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 1 int enqueue(int value) { list_element_t *newle = (list_element_t *)malloc(sizeof(list_element_t)); if (newle == 0) return 0; newle->value = value; newle->next = 0; if (head == 0) { // list was empty assert(tail == 0); head = newle; else { tail->next = newle; tail = newle; This version of enqueue makes unnecessary the call to free in dequeue. a) It works well. b) It fails occasionally. c) It hardly every works. d) It never works. free(newle); // saves us the bother of freeing it later return 1; CS33 Intro to Computer Systems XXVI 7 Copyright 2017 Thomas W. Doeppner. All rights reserved.

malloc and free void *malloc(size_t size) allocate size bytes of storage and return a pointer to it returns 0 (NULL) if the requested storage isn t available void free(void *ptr) free the storage pointed to by ptr ptr must have previously been returned by malloc (or other storage-allocation routine calloc and realloc) CS33 Intro to Computer Systems XXVI 8 Copyright 2017 Thomas W. Doeppner. All rights reserved.

realloc void *realloc(void *ptr, size_t size) change the size of the storage pointed to by ptr the contents, up to the minimum of the old size and new size, will not be changed ptr must have been returned by a previous call to malloc, realloc, or calloc it may be necessary to allocate a completely new area and copy from the old to the new» thus the return value may be different from ptr» if copying is done the old area is freed returns 0 if the operation cannot be done CS33 Intro to Computer Systems XXVI 9 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Get (contiguous) Input (1) char *getinput() { int alloc_size = 4; // start small int read_size = 4; int next_read = 0; int bytes_read; // max number of bytes to read // index in buf of next read // number of bytes read char *buf = (char *)malloc(alloc_size); char *newbuf; if (buf == 0) { // no memory return 0; CS33 Intro to Computer Systems XXVI 10 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Get (contiguous) Input (2) while (1) { if ((bytes_read = read(0, buf+next_read, read_size)) == -1) { perror("getinput"); return 0; if (bytes_read == 0) { // eof, possibly premature return buf; if ((buf+next_read)[bytes_read-1] == '\n') { // end of line break; CS33 Intro to Computer Systems XXVI 11 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Get (contiguous) Input (3) next_read += read_size; read_size = alloc_size; alloc_size *= 2; newbuf = (char *)realloc(buf, alloc_size); if (newbuf == 0) { // realloc failed: not enough memory. // Free the storage allocated previously and report // failure free(buf); return 0; buf = newbuf; CS33 Intro to Computer Systems XXVI 12 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Get (contiguous) Input (4) // reduce buffer size to the minimum necessary newbuf = (char *)realloc(buf, alloc_size - (read_size - bytes_read)); if (newbuf == 0) { // couldn't allocate smaller buf return buf; return newbuf; CS33 Intro to Computer Systems XXVI 13 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Some Common Memory- Related Errors CS33 Intro to Computer Systems XXVI 14 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dereferencing Bad Pointers The classic scanf bug int val;... scanf("%d", val); CS33 Intro to Computer Systems XXVI 15 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Reading Uninitialized Memory Assuming that dynamically allocated data is initialized to zero /* return y = Ax */ int *matvec(int A[][N], int x[]) { int *y = (int *)malloc(n*sizeof(int)); int i, j; for (i=0; i<n; i++) for (j=0; j<n; j++) y[i] += A[i][j]*x[j]; return y; CS33 Intro to Computer Systems XXVI 16 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Overwriting Memory Allocating the (possibly) wrong-sized object // set up p so it is an array of // int * s, allocated dynamically int **p; p = (int **)malloc(n*sizeof(int)); for (i=0; i<n; i++) { p[i] = (int *)malloc(m*sizeof(int)); p[0] p[1] p[2] p[3] p[4] p[5] p[6] p[7] CS33 Intro to Computer Systems XXVI 17 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Overwriting Memory Not checking the max string size char s[8]; int i; gets(s); /* reads 123456789 from stdin */ Basis for classic buffer overflow attacks CS33 Intro to Computer Systems XXVI 18 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Going Too Far Misunderstanding pointer arithmetic int *search(int p[], int val) { while (*p && *p!= val) p += sizeof(int); return p; CS33 Intro to Computer Systems XXVI 19 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Referencing Nonexistent Variables Forgetting that local variables disappear when a function returns int *foo () { int val; return &val; CS33 Intro to Computer Systems XXVI 20 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Freeing Blocks Multiple Times x = (int *)malloc(n*sizeof(int)); <manipulate x> free(x); y = (int *)malloc(m*sizeof(int)); <manipulate y> free(x); CS33 Intro to Computer Systems XXVI 21 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Referencing Freed Blocks x = (int *)malloc(n*sizeof(int)); <manipulate x> free(x);... y = (int *)malloc(m*sizeof(int)); for (i=0; i<m; i++) y[i] = x[i]++; CS33 Intro to Computer Systems XXVI 22 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Failing to Free Blocks (Memory Leaks) foo() { int *x = (int *)malloc(n*sizeof(int)); Use(x, N); return; CS33 Intro to Computer Systems XXVI 23 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Failing to Free Blocks (Memory Leaks) Freeing only part of a data structure struct list { int val; struct list *next; ; foo() { struct list *head = malloc(sizeof(struct list)); head->val = 0; head->next = NULL; <allocate and manipulate the rest of the list>... free(head); return; CS33 Intro to Computer Systems XXVI 24 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Total Confusion foo() { char *str; str = (char *)malloc(1024);... str = "";... strcat(str, "c");... return; CS33 Intro to Computer Systems XXVI 25 Copyright 2017 Thomas W. Doeppner. All rights reserved.

It Works, But... Using a hammer where a feather would do... hammer() { int *x = (int *)malloc(1024*sizeof(int)); Use(x, 1024); free(x); return; feather() { int x[1024]; Use(x, 1024); return; CS33 Intro to Computer Systems XXVI 26 Copyright 2017 Thomas W. Doeppner. All rights reserved.