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

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.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
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 CE Lecture 19 Linear Linked Lists.
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.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
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,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Introduction to Data Structures Systems Programming Concepts.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
1 Homework / Exam Finishing K&R Chapter 5 today –Skipping sections for now –Not covering section 5.12 Starting K&R Chapter 6 next Continue HW5.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS261 Data Structures Linked Lists - Introduction.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Dynamic Allocation Review Structure and list processing
5.13 Recursion Recursive functions Functions that call themselves
Computer Science 210 Computer Organization
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Introduction to Programming
Data Structures and Algorithms
malloc(): Dynamic Memory Management
Checking Memory Management
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
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) {
Chapter 16-2 Linked Structures
Circular Buffers, Linked Lists
Pointers.
Computer Science 210 Computer Organization
Dynamic Memory Allocation
Pointers.
Review & Lab assignments
Data Structures and Algorithms
Dynamic Memory A whole heap of fun….
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
7. Pointers, Dynamic Memory
Dynamic Memory A whole heap of fun….
C Programming Lecture-8 Pointers and Memory Management
Chapter 10-1: Dynamic Memory Allocation
Pointers.
Dynamic Data Structures
Presentation transcript:

Dynamic Allocation and Linked Lists

Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is defined in stdlib.h and malloc.h header files. malloc returns a void pointer. the pointer returned from malloc requires a type cast to make it usable.

Type Casting To make one data type appear like another you can use a type cast. int I; int a = (int)sqrt((float) I ); To make the void pointer returned by malloc look like your struct you must typecast it to a pointer to a struct.

Example struct rec { char LastName[41]; charFirstName[41]; }; struct rec *r; r = (struct rec) malloc( sizeof(struct rec));

Freeing Memory Memory allocated with malloc must be released after you are done with them. this memory is released by calling the function free(). Since free expects as an argument the pointer returned by malloc it expects a void pointer. free( r );

Linked List A linked list is a basic data structure. For easier understanding divide the linked list into two parts. Nodes make up linked lists. Nodes are structures made up of data and a pointer to another node. Usually the pointer is called next.

Nodes struct node { struct rec r; struct node *next; };

LIST The list will contain a pointer to the start of the list. For our purposes all data will be added to the start of the list. Initialize the start pointer to NULL.

Creating a New Node Allocate space for a new node. Set the next pointer to the value of NULL Set the data value for the node.

Adding Nodes to the List If the start node is null then the start node becomes the new node. If start is not null then start becomes the new node’s next and the start becomes the new node.