Dynamic memory allocation in C and C++

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

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.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
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.
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:
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.
Dynamic memory allocation in C and C++ 程式設計 潘仁義 CCU COMM.
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.
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
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.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
資料結構(計財系).
© 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.
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.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
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.
LINKED LISTS.
Dynamic Allocation Review Structure and list processing
Computer Science 210 Computer Organization
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
ENEE150 Discussion 07 Section 0101 Adam Wang.
Data Structures and Algorithms
Traversing a Linked List
CSCI206 - Computer Organization & Programming
Chapter 14: Dynamic Data Structures
CSCE 210 Data Structures and Algorithms
Dynamic memory allocation in C and C++
14th September IIT Kanpur
Chapter 16-2 Linked Structures
LINKED LISTS.
קורס תכנות שיעור 13: רשימות מקושרות.
Computer Science 210 Computer Organization
Structure and Union Types
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Review & Lab assignments
Data Structures and Algorithms
Pointers & Dynamic Data Structures
7. Pointers, Dynamic Memory
Dynamic Memory A whole heap of fun….
Dynamic Memory.
CS148 Introduction to Programming II
Dynamic memory allocation in C and C++
Structures EECS July 2019.
CS148 Introduction to Programming II
Dynamic Memory – A Review
Module 13 Dynamic Memory.
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 <stdlib.h> 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 { char current[3]; int volts; 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所指的地方 */

可用 n2_p->volts n3_p->volts 或 n1_p->linkp->volts 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