Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
Dynamic memory allocation
Advertisements

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.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts / : Introduction to Computer Systems 18 th Lecture, March 24, 2015 Instructors:
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
1 Memory Allocation Professor Jennifer Rexford COS 217.
User-Level Memory Management in Linux Programming
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Kernighan/Ritchie: Kelley/Pohl:
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{
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
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.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
CS 11 C track: lecture 5 Last week: pointers This week: Pointer arithmetic Arrays and pointers Dynamic memory allocation The stack and the heap.
Outline Midterm results Static variables Memory model
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Stack and Heap Memory Stack resident variables include:
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
University of Washington Today Finished up virtual memory On to memory allocation Lab 3 grades up HW 4 up later today. Lab 5 out (this afternoon): time.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 17 Free-Space Management Chien-Chung Shen CIS, UD
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Chapter 17 Free-Space Management
CprE 185: Intro to Problem Solving (using C)
Stack and Heap Memory Stack resident variables include:
Dynamic Allocation Review Structure and list processing
Day 03 Introduction to C.
Lectures linked lists Chapter 6 of textbook
Dynamic Memory Allocation
Day 03 Introduction to C.
CSCI206 - Computer Organization & Programming
The Hardware/Software Interface CSE351 Winter 2013
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
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) {
CprE 185: Intro to Problem Solving (using C)
CSC 253 Lecture 8.
CSC 253 Lecture 8.
CprE 185: Intro to Problem Solving (using C)
Dynamic Memory Allocation
Memory Allocation CS 217.
EECE.2160 ECE Application Programming
Review & Lab assignments
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Programming in C Advanced Pointers.
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Dynamic Data Structures CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Administrative Stuff There will be labs this week (also TA evaluations) This Wednesday we’ll have a review for the final

Final Exam Final Exam: Thursday Dec 17: 2:15- 4:15 pm Location: This room. The final is worth 25% of your grade. The best way to fail this class is to not show up.

Final Format True/False(10 x 1p each = 10p) Fun Stuff( 3 x 5p each = 15p) Short answers( 5 x 3p each = 15p) Code Snippets( 4 x 5p each = 20p) What is the output( 3 x 5p each = 15p) Program 1(15p) Program 2(20p) Program 3(20p) TOTAL (130p)

Final Format You don’t need to get all 130 points to get an A 100 is a 100 You must get at least 65 points in order to pass this exam

How do I clear the screen? #include int main() { printf("%d\n", 34523); system("cls"); // system call system("pause"); }

HW 10: Student Structure #include typedef struct { int a; } schedule_t; typedef struct { int b; schedule_t schedule[6]; } student_t; int main() { system("pause"); }

Chapter (14)

What’s going on inside the computer’s memory [

Dynamic Memory “In computer science, dynamic memory allocation is the allocation of memory storage for use in a computer program during the runtime of that program. It can be seen also as a way of distributing ownership of limited memory resources among many pieces of data and code.” -From Wikipedia

Dynamic Memory “Dynamically allocated memory exists until it is released either explicitly by the programmer, exiting a block, or by the garbage collector. This is in contrast to static memory allocation, which has a fixed duration. It is said that an object so allocated has a dynamic lifetime.” -From Wikipedia

[

Each program uses several “types” of memory program stack shared library objects heap Some of these are read only, others grow dynamically on as needed basis.

NOTE: The following slides are QNX specific but the same principles apply to almost any other modern operating system Source:

Process memory layout on an x86. [

Program Memory [

Organization of a program’s memory [

Stack Memory The Stack memory holds the local variables and parameters your program's functions use. [

Shared-library Memory [

Object memory “Object memory represents the areas that map into a program's virtual memory space, but this memory may be associated with a physical device. For example, the graphics driver may map the video card's memory to an area of the program's address space: “ [

Heap memory [ “Heap memory represents the dynamic memory used by programs at runtime. Typically, processes allocate this memory using the malloc(), realloc(), and free() functions.”malloc()realloc()free()

The malloc function #include stdlib.h void *malloc(size_t size); The malloc function allocates size number of bytes in the heap memory and returns a pointer to that memory. Note: The return value is a void pointer so we must cast it to a pointer of the type that we want.

Using malloc To allocate an int: int* ip; ip = (int*) malloc( sizeof(int) ); To allocate a char: char* cp = (char*) malloc( sizeof(char) ); To allocate a struct: struct node* np = (struct node*) malloc( sizeof(struct node) );

How malloc(..) works [

How malloc(..) works “When the malloc library receives an allocation request that it can't meet with its existing heap, the library requests additional physical memory from the process manager. As your program frees memory, the library merges adjacent free blocks to form larger free blocks wherever possible. If an entire memory page becomes free as a result, the library returns that page to the system. The heap thus grows and shrinks in 4K increments” [

The free function Syntax: void free ( void * ptr ); Example: int* ip; ip = (int*) malloc( sizeof(int) ); free (ip);

Linked Lists

What is the meaning of this? typedef struct node { int value; struct node *next; } node_t; valuenext ?

Example: Dynamically Allocated Struct #include typedef struct node { int value; struct node *next; } node_t; int main() { // pointer to a dynamically allocated struct node_t *q = (node_t*) malloc (sizeof(node_t)); q->value = 10; q->next = NULL; printf("value = %d\n", q->value); system("pause"); }

What is the meaning of this code? #include typedef struct node { int value; struct node *next; } node_t; int main() { node_t *p = (node_t*) malloc (sizeof(node_t)); p->value = 37; node_t *q = (node_t*) malloc (sizeof(node_t)); q->value = 99; node_t *r = (node_t*) malloc (sizeof(node_t)); r->value = 12; p->next = NULL; q->next = p; r->next = q; }

Singly-Linked List [

Inserting a node into a Linked List BeforeAfter [

Deleting a node from a linked list Before: After: [

Doubly-Linked List [

The node struct has 2 pointers typedef struct { int value; node_t next; node_t prev; } node_t;

Inserting a node into a doubly-linked list BeforeAfter [

Circularly-linked list [

Algorithm animations

THE END