Dynamic memory allocation in C and C++ 程式設計 潘仁義 CCU COMM.

Slides:



Advertisements
Similar presentations
Linked Lists Mohammed Almashat CS /03/2006.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Linked Lists.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
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.
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.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
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:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { char name[10];
Topic 12 – Dynamic Memory Allocation & Linked Lists
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
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.
Structure and Union Types 程式設計 潘仁義 CCU COMM. Structure Type Definition struct structured data objects, can be defined by users #define STRSIZ 10 typedef.
Chapter 14 Dynamic Data Structures Instructor: Alkar & Demirer.
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 };
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 ( 台大資工 趙坤茂 )
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
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.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
1 Writing a Good Program 8. Elementary Data Structure.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
One-dimensional arrays and strings: Chapter 6, Slide 1 The concept of array - an extension of the basic model of memory:
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
資料結構(計財系).
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Problem Solving & Program Design in C Seventh Edition By Jeri R. Hanly.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Dynamic Allocation Review Structure and list processing
UNIT – I Linked Lists.
Traversing a Linked List
CSCI206 - Computer Organization & Programming
Chapter 14: Dynamic Data Structures
Dynamic memory allocation in C and C++
14th September IIT Kanpur
Chapter 16-2 Linked Structures
קורס תכנות שיעור 13: רשימות מקושרות.
Structure and Union Types
Dynamic Memory Allocation
Review & Lab assignments
7. Pointers, Dynamic Memory
CS148 Introduction to Programming II
Dynamic memory allocation in C and C++
Dynamic memory allocation in C and C++
Dynamic Data Structures
Presentation transcript:

Dynamic memory allocation in C and C++ 程式設計 潘仁義 CCU COMM

Pointers void main () { int num = 3; int *nump = # …..

Dynamic memory allocation in C (1/3) void some_function () { int *nump; char *letp; planet_t *planetp; …

Dynamic memory allocation in C (2/3) void some_function () { int *nump; char *letp; planet_t *planetp; … #include nump=(int*) malloc(sizeof(int)); letp = (char*) malloc(sizeof(char)); planetp = (planet_t *) malloc(sizeof(planet_t));

Dynamic memory allocation in C (3/3) void some_function () { int *nump; char *letp; planet_t *planetp; … nump=(int*) malloc(sizeof(int)); letp = (char*) malloc(sizeof(char)); planetp = (planet_t *) malloc(sizeof(planet_t)); *nump = 307; *letp = ‘Q’; *planetp = blank_planet;

Allocation of Arrays with calloc

Allocation of Arrays with calloc (cont’d)

Returning cells to the Heap … free(letp); free(nump); free(planetp); … free(string1); free(array_of_nums); free(array_of_planets); double *xp, *xcopyp; xp =(double *)malloc(sizeof(double)); *xp = 49.5; xcopyp = xp; free(xp); 『 *xcopyp 』 should not be used after it freed or errors can result

Dynamic memory allocation in C++ int *nump; char *letp; planet_t *planetp; char *string1; int *array_of_nums; planet_t *array_of_planets; nump= new int; letp = new char; planetp = new planet_t; string1 = new char[str_size]; array_of_nums = new int[num_nums]; array_of_planets = new planet_t[num_planets]; delete nump; delete letp; delete planetp; delete [ ] string1; delete [ ] array_of_nums; delete [ ] array_of_planets;

Linked list Children’s Pop Beads in a Chain

Linked list Structures with pointer components typedef struct node_s { charcurrent[3]; intvolts; struct node_s *linkp; } node_t; node_t *n1_p, *n2_p, *n3_p; n1_p = (node_t *)malloc(sizeof(node_t)); n1_p->volts = 115; n2_p = (node_t *)malloc(sizeof(node_t)); n2_p->volts = 12; n3_p = n2_p; /* 令 n3_p 指向 n2_p 所指的地方 */

Linking Two Nodes n1_p->linkp = n2_p; /* 令上面的 linkp 指向下面 */ 可用 n2_p->volts n3_p->volts 或 n1_p->linkp->volts

Three-Node Linked List n2_p->linkp = (node_t *) malloc(sizeof(node_t)); n2_p->linkp->volts=220; strcpy(n2_p->linkp->current, “AC”); n2_p->linkp->linkp = NULL;

Linked list operations After an Insertion After a Deletion

Common programming error var->component is valid only if var is of a pointer-to-structure The most common error is an attempt to follow a NULL pointer Checking before access Do not attempt to reference a node after freeing it