Heap Arrays and Linked Lists. Steven R. Bagley

Similar documents
Heap Arrays. Steven R. Bagley

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

CS 11 C track: lecture 5

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

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

Data Structure Series

Intermediate Programming, Spring 2017*

POINTER AND ARRAY SUNU WIBIRAMA

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

EL2310 Scientific Programming

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Dynamic Memory Allocation and Linked Lists

Writing Functions in C

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

CS61C Midterm Review on C & Memory Management

My malloc: mylloc and mhysa. Johan Montelius HT2016

Dynamic Data Structures. CSCI 112: Programming in C

Lecture 8 Dynamic Memory Allocation

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

Kurt Schmidt. October 30, 2018

Lectures 13 & 14. memory management

Singly linked lists in C.

CS61C : Machine Structures

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

CS61C : Machine Structures

CS61C : Machine Structures

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

CS61C : Machine Structures

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

CS 222: Pointers and Manual Memory Management

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

a data type is Types

CSC 1600 Memory Layout for Unix Processes"

First of all, it is a variable, just like other variables you studied

CS107, Lecture 9 C Generics Function Pointers

Class Information ANNOUCEMENTS

Memory (Stack and Heap)

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

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

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

Understanding Pointers

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

C Pointers. ENGG1002 Computer Programming and Applica;ons Dr. Hayden Kwok Hay So Week 2. GeGng Hold of Memory. Sta;c: When we define a variable

211: Computer Architecture Summer 2016

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Structures and Pointers

Pointers, Dynamic Data, and Reference Types

However, in C we can group related variables together into something called a struct.

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

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

Advanced Pointer Topics

Chapter 1 Getting Started

REFERENCES, POINTERS AND STRUCTS

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Dynamic Memory Allocation

Instructions: Submit your answers to these questions to the Curator as OQ02 by the posted due date and time. No late submissions will be accepted.

Arrays and Memory Management

Welcome! COMP s1. Programming Fundamentals

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Pointers in C/C++ 1 Memory Addresses 2

EL2310 Scientific Programming

Recitation #11 Malloc Lab. November 7th, 2017

Reminder: compiling & linking

Section Notes - Week 1 (9/17)

Character Strings. String-copy Example

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

Memory and Addresses. Pointers in C. Memory is just a sequence of byte-sized storage devices.

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS24 Week 2 Lecture 1

Dynamically Allocated Memory in C

Introduction to Linked Lists

Pointers (continued), arrays and strings

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

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

BBM 201 DATA STRUCTURES

CS 237 Meeting 19 10/24/12

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

CS 61c: Great Ideas in Computer Architecture

Lecture Notes on Memory Management

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

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

CA31-1K DIS. Pointers. TA: You Lu

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Dynamic Allocation of Memory

CS 103 Unit 11. Linked Lists. Mark Redekopp

Pointers (continued), arrays and strings

EL2310 Scientific Programming

Midterm Review. CS 211 Fall 2018

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Pointers and scanf() Steven R. Bagley

G52CPP C++ Programming Lecture 9

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 16 CSE July 1992

Lecture Notes on Memory Management

Transcription:

Heap Arrays and Linked Lists Steven R. Bagley

Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index Variables and arrays have a type Create our own data structures Allocate our own memory

Allocating memory int *p = (int *)malloc(sizeof(int)); Find the size of a int malloc that many bytes Cast the void * pointer to a int * And store in p This generally safe, although handle with care Always think why am I casting this? Don t cast just to make the errors and warnings go away

Using allocated memory Can use this allocated int like any other pointer to an int But have to access it via the pointer Cannot obtain a variable name for it Can also allocate structs in this fashion

free() When we ve finished with allocated memory, we need to free() it Otherwise, it can t be used for anything else until the program quits But can t free it until we ve finished with it (the program has no more pointers with it) Otherwise, accessing it will cause problem

free() void free(void *ptr) Pass it the pointer to the memory you want freeing Must be allocated via malloc() Afterwards, the variable containing the pointer should be cleared as it is no longer valid

Allocating struct *p = ( *) malloc(sizeof()); Find the size of a malloc that many bytes Cast the void * pointer to a * And store in p This generally safe, although handle with care Always think why am I casting this? Don t cast just to make the errors and warnings go away

Heap structs Can use these allocated blocks just like anything else we ve pointed at p->x = 42.0; p->y = 36.0; pt = *p; Allocating structs on the heap is used a lot Most data is stored on the heap

Arrays on the Heap Arrays are represented by a pointer to the base of the array Each element in the array is laid out in memory sequentially The array operator [] works just as well on a pointer as on an array

Arrays on the Heap If we can malloc space for one thing Then we can malloc space for two things Or three, or four etc Treat that pointer as the base of the array

Arrays on the Heap Find out how big one thing is using sizeof() If we want to allocate space for n things Multiple sizeof() by n Gives us the number of bytes to allocate So an array of 42 ints: int *p = malloc(42 * sizeof(int));

Solving our pictures Don t decide array size at compile-time Create it at run-time using malloc() But how big should it be? Four options

Size of Path Array Option One read through the file twice First time through, count lines until stop Then allocate memory using malloc Then use fseek to go back to the beginning and read again Problem might be reading from something that can t be seeked (e.g stdin)

Size of Path Array Option Two Cheat Rather than putting stop at the end Make the first line contain the number of elements Read that Use value to allocate the array

Size of Path Array Option Three copy data if array gets full Allocate the array of a certain size If we fill it, allocate a new larger array And copy the data over using memcpy Works, but can leave memory fragmented and slows program while copying

Size of Path Array Option Four Don t use an Array The problem we have is that an array s size is fixed when created If it needs to grow, we need to create a new one and copy the data So use a different data structure that doesn t have this limitation, such as

SET / SEM As in G51CSA, you get a chance to give feedback on the module Be careful with the scale Done online at http://bluecastle.nottingham.ac.uk

The Linked List

Linked List Ordered collection Size not fixed Grows and shrinks as elements are added Add/remove elements from the list easily Trickier to access a particular element in the list (sequential access rather than random access)

Linked List vs Array Linked List Array Ordered Grows/shrinks on demand Easily add/remove elements Sequential access Fixed size Difficult to add/remove elements Random Access

Linked List Linked lists are built using structs These structs store the data as variables inside them just as the others we ve seen But they also have another variable A pointer to the item in the list (sometimes called the link) Last item points at nothing (i.e. NULL)

listhead Element intval 42 Element intval 21 Element intval 16 Element intval 8 Last struct, doesn t point at anything (null) to signify the end of the list

Linked List Have to use pointers Have to allocate the memory for the structs using malloc Store pointer to the first struct (at least) Easy to access first element (we have a pointer to it) If we didn t use pointers the struct would fill up the computers memory

Accessing an element To access the n th element Start at the head of the list This refers to the first element Follow the link to the element This is the second element And so on until you reach the n th element

Linked List of points How could we make a linked list out of our points? Still need the variables in the struct as we had before Add a new variable a pointer to a

Linked List of points { float x; float y; *; };

Linked List of points Allocate memory for this struct using malloc Set data in the normal way Also set to null Store pointer in a variable that points to the first item Nothing unusual

Creating a Second Point Allocate memory this struct using malloc Set data in the normal way Also set to null Where do we store this pointer? In the variable of the first point

General case Unless it is the first item (point in this case) We have to know where the first item is The pointer to an item goes in the previous item s variable Can then find any item by following the links from the first item

Adding to the end Often need to add a struct to the end of the list Three stage process First, allocate space for the new struct Second, find the last struct in the list Third, set the last struct s to point to the new struct Last struct is the one where is equal to NULL

Allocating struct Seen this already use malloc() Must be allocated on the heap, no other way to do it Set the variables Set to be NULL

Finding the last element Relatively straightforward Start at the first item Follow the pointers until we find a struct where is NULL Special case when list is empty (head is NULL)

General case When head is not NULL: Set a pointer, p, to equal head If p-> is NULL, stop as end found Otherwise, set p to equal p-> (moves p to point to the thing in list) Repeat until end found

General case When we reach the termination case, p will be pointing at the last item in the list Because we test whether p-> is NULL and not p itself Can then set p-> to point to our newly allocated struct

Special case If head is NULL, then previous algorithm will crash Will try to dereference p when it is NULL Need to treat this case differently Simple, just check whether head is NULL If so, set it to point to new struct

head

head x 100.0

head x 100.0

head x 100.0 x 300.0

head p x 100.0 x 300.0

head p x 100.0 x 300.0

head x 100.0 x 300.0

head x 100.0 x 300.0 x 300.0 y 300.0

head p x 100.0 x 300.0 x 300.0 y 300.0

head p x 100.0 x 300.0 x 300.0 y 300.0

head p x 100.0 x 300.0 x 300.0 y 300.0

head x 100.0 x 300.0 x 300.0 y 300.0

head x 100.0 x 300.0 x 300.0 y 300.0

head x 100.0 x 300.0 x 300.0 x 100.0 y 300.0 y 300.0

head p x 100.0 x 300.0 x 300.0 x 100.0 y 300.0 y 300.0

head p x 100.0 x 300.0 x 300.0 x 100.0 y 300.0 y 300.0

head p x 100.0 x 300.0 x 300.0 x 100.0 y 300.0 y 300.0

head p x 100.0 x 300.0 x 300.0 x 100.0 y 300.0 y 300.0

head x 100.0 x 300.0 x 300.0 x 100.0 y 300.0 y 300.0

Adding at the end Quite slow to add things at the end of a linked list Each item slows down adding the one since we have to visit one more item Can speed it up by keeping a pointer to the last item as well