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
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

C++ crash course Class 10 practice problems. Pointers The following function is trying to swap the contents of two variables. Why isnt it working? void.
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
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
C Intro.
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.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
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.
1 Objectives ❏ To understand the relationship between arrays and pointers ❏ To understand the design and concepts behind pointer arithmetic ❏ To write.
C Lab 3 C Arraylist Implementation. Goals ●Review ○ Referencing/Dereferencing ○ Free ●realloc and memmove ●ArrayList ●Debugging with GDB.
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.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
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{
Informática II Prof. Dr. Gustavo Patiño MJ
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Computer Science 210 Computer Organization Pointers and Dynamic Storage.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Week 9 Part 2 Kyle Dewey. Overview Announcement More with structs and memory Assertions Exam #2 Course review.
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 ( 台大資工 趙坤茂 )
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,
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
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
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Homework HW5 due today Review a lot of things about allocation of storage that may not have been clear when we covered them in our initial pass Introduction.
UFS003C3 Lecture 15 Data type in C & C++ Using the STL.
ECE Application Programming
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.
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.
CSE 351 Dynamic Memory Allocation 1. Dynamic Memory Dynamic memory is memory that is “requested” at run- time Solves two fundamental dilemmas: How can.
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.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Computer Science 210 Computer Organization
Introduction to Programming
malloc(): Dynamic Memory Management
CSCI206 - Computer Organization & Programming
Programming Languages and Paradigms
Computer Science 210 Computer Organization
Dynamic Memory Allocation
Review & Lab assignments
CS111 Computer Programming
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
Dynamic Memory Allocation
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 header files. malloc returns a void pointer. the pointer returned from malloc requires a type cast to make it usable. (void *: just a memory address)

Null Pointers When a memory allocation function is called, theres always a possibility that it wont be able to locate a block of memory large enough to satisfy the requres. A null pointer will be returned. Test the return value: p = malloc(10000); if(p == NULL){ }

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(). free expects as an argument the pointer returned by malloc. free( r );

Allocate Memory for Arrays int *p; // one dimension int **q; // tow dimension int i; p = (int *) malloc( sizeof(int) * n); q = (int **) malloc( sizeof(int*) * n); for(i=0; i<n; ++i) { q[i] = (int *) malloc( sizeof(int) * n); } //…… free(p); free(q);

The Dangling Pointer Problem char *p = malloc(4); …. free(p); …. strcpy(p, abc); // WRONG

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 nodes next and the start becomes the new node.

Deleting a node P Q I want to delete Q R

Deleting a node P Q I want to delete Q R P->next = Q->next; Q->next = NULL; Free(Q);