ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Dynamic memory allocation
Data Structure Lecture-5
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 31: Dynamic memory allocation.
Managing Memory Static and Dynamic Memory Type Casts Allocating Arrays of Dynamic Size Resizing Block of Memory Returning Memory from a Function Avoiding.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Introduction of Memory Allocation. Memory Allocation There are two types of memory allocations possible in c. Compile-time or Static allocation Run-time.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
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:
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Introduction to C Programming in Unix Environment - II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015 Some slides.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
CSE1303 Part A Data Structures and Algorithms Lecture A6 – Dynamic Memory.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A6 – Dynamic Memory.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Lecture 13 Static vs Dynamic Memory Allocation
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
Stack and Heap Memory Stack resident variables include:
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
ENEE150 – 0102 ANDREW GOFFIN Linked List/Project 3.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
6-1 Embedded Systems C Programming Language Review and Dissection IV Lecture 6.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Stack and Heap Memory Stack resident variables include:
Chapter 12 – Data Structures
Introduction to Programming
ENEE150 Discussion 07 Section 0101 Adam Wang.
CSCI206 - Computer Organization & Programming
Some examples.
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Pointers and dynamic memory
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
Review & Lab assignments
CS111 Computer Programming
C Programming Lecture-8 Pointers and Memory Management
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory

Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated memory during runtime Goffin – ENEE150 Static  On the stack  Amount of memory chosen at compiletime  Memory is allocated for entirety of runtime

Dynamic Memory Motivation More optimized data structures Optimal memory usage More powerful data structures overall  Linked lists, binary search trees, etc. Goffin – ENEE150

Dynamic Allocation A few possible functions to allocate memory Three main ones: void *malloc(int)  most important!!! void *calloc(int, int) void *realloc(void *, int) These functions are all in stdlib.h Goffin – ENEE150

Malloc Parameter determines how many contiguous bytes of memory to allocate  % of time you use sizeof( ) Good to explicitly cast output of malloc, but not necessary Example: int *x = (int *)malloc(sizeof(int));  Allocates one “integer size” of memory and stores the address of the first byte to the pointer x  Explicit cast with (int *)  If you want an array, multiply sizeof(int) by array size Goffin – ENEE150

Notes about Malloc When there is no memory, malloc will return a NULL pointer  ALWAYS check for this, otherwise program will go haywire Should use free() for all dynamically allocated memory Goffin – ENEE150

Calloc and Realloc Calloc assumes you want an array and takes two arguments: size of one element and number of elements int *a = (int *)calloc(sizeof(int), 5); // allocates memory for int array of size 5 Realloc takes dynamically allocated memory and changes how much of it there is  Good for increasing/decreasing length of arrays, for example int *b = (int *)realloc(a, 3); // returns pointer to a but with int array of only size 3 Goffin – ENEE150

Intro to Linked Lists Essentially a more dynamic array! Goffin – ENEE150

Some Linked List Vocab Node  Each element in the list: stores data and a “next pointer” Head pointer  Points to first element in the linked list  Is NULL for an empty list Next pointer  Pointer in each node that indicates the next node in the list  Is NULL for the last element in the list Tail pointer (if applicable)  Points to last element in the linked list  Is NULL for an empty list Goffin – ENEE150

Defining a linked list Use a struct to define a single node in the list typedef struct node_type{ // type of data depends on application! int data1; float data2; // NEXT POINTER, SUPER IMPORTANT struct node_type *next; } node, *pnode; Goffin – ENEE150

Starting a linked list int main(void){ pnode head = NULL; head = (pnode)malloc(sizeof(node)); head->next = NULL; } A single node is allocated Next pointer is NULL, indicating that the node is the last (and only) node in the list Goffin – ENEE150