dynamic memory allocation in c and c++

15
Dynamic memory allocation Dynamic memory allocation in C and C++ in C and C++ 程程程程 程程程 CCU COMM

Upload: lorie

Post on 12-Jan-2016

69 views

Category:

Documents


0 download

DESCRIPTION

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). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Dynamic memory allocation in C and C++

Dynamic memory allocationDynamic memory allocationin C and C++in C and C++

程式設計潘仁義

CCU COMM

Page 2: Dynamic memory allocation in C and C++

PointersPointers

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

Page 3: Dynamic memory allocation in C and C++

Dynamic memory allocation in C (1/3)Dynamic memory allocation in C (1/3)

void some_function () { int *nump;char *letp;planet_t *planetp;…

Page 4: Dynamic memory allocation in C and C++

Dynamic memory allocation in C (2/3)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));

Page 5: Dynamic memory allocation in C and C++

Dynamic memory allocation in C (3/3)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;

Page 6: Dynamic memory allocation in C and C++

Allocation of Arrays with callocAllocation of Arrays with calloc

Page 7: Dynamic memory allocation in C and C++

Allocation of Arrays with calloc Allocation of Arrays with calloc (cont’d)(cont’d)

Page 8: Dynamic memory allocation in C and C++

Returning cells to the HeapReturning 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;xcopy = xp;free(xp);

『 *xcopy 』 should not be used after it freed or errors can result

Page 9: Dynamic memory allocation in C and C++

Dynamic memory allocation in C++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;

Page 10: Dynamic memory allocation in C and C++

Linked listLinked listChildren’s Pop Beads in a ChainChildren’s Pop Beads in a Chain

Page 11: Dynamic memory allocation in C and C++

Linked listLinked listStructures with pointer componentsStructures 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 所指的地方 */

Page 12: Dynamic memory allocation in C and C++

Linking Two Nodes Linking Two Nodes

n1_p->linkp = n2_p; /* 令上面的 linkp 指向下面 */

可用n2_p->voltsn3_p->volts

或n1_p->linkp->volts

Page 13: Dynamic memory allocation in C and C++

Three-Node Linked ListThree-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;

Page 14: Dynamic memory allocation in C and C++

Linked list operationsLinked list operations

After an Insertion

After a Deletion

Page 15: Dynamic memory allocation in C and C++

Common programming errorCommon programming error

var->componentis valid only if var is of a pointer-to-structure

The most common error isan attempt to follow a NULL pointer

Checking before access

Do not attempt to reference a node after freeing it