managing memory dct 1063 programming 2 mohd nazri bin ibrahim faculty of computer, media &...

23
Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College [email protected]

Upload: peter-mitchell

Post on 15-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

Managing Memory

DCT 1063 PROGRAMMING 2

Mohd Nazri Bin IbrahimFaculty of Computer, Media & TechnologyTATi University [email protected]

Page 2: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.1 INTRODUCTION

So far, fixed amount of memory is usedProgram could not increase or decreased

memory during runtimeDynamic memory allow you to create and

use data structure that can grow and shrink.

Page 3: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.2 Static and Dynamic Memory

With static memory, you define the maximum amount of space required.char a[1000];

There no way to change amount of memory in runtime.

Dynamic memory comes in blocks without names, just address.

Page 4: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.2 Static and Dynamic Memory (cont.)

malloc() function is used to request a block of certain size (in byte).

int memsize;

// memsize get a value at runtime

x=malloc(memsize);

Page 5: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.2 Static and Dynamic Memory (cont.)

free() function is used to return it (memory) to the pool.

free(x);

Every call to malloc() must be balanced by a call to free().

Page 6: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.2 Static and Dynamic Memory (cont.)

malloc() return the starting address of the newly allocated block.

The starting address to be stored in pointer of type void;

Void *x;

A void pointer variable can store address of any type.

Refer to figure 10.1

Refer to script 10.1

Page 7: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

Figure 10.1

x memsize

24 120 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

Page 8: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

/* memory.c - Script 10.1 */

#include <iostream.h>#include <stdlib.h>int main (void)

{ /* Declare a void pointer variable. */void *x;/* Request a block of memory with 1000 bytes. */x = malloc (1000);

/* Print out the starting address of the memory block. */cout<<"Starting block address: “<< x;

/* Return the block to the pool. */free(x); x = NULL;

/*Pause and wait for input before terminating. *///getchar();return 0;

}

Page 9: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.2 Static and Dynamic Memory (cont.)

Void *x;

X= malloc(size);

Free(x);

X=NULL;

The pointer should be set to NULL after freed.

Page 10: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.3 TYPE CAST

The void pointer variable allows you to store and retrieve any kind of address (integer, float, double, string etc).

Since the pointer can point to anything, the system cannot know if it should read or write a 1-byte or a 4-byte integer, or something completely different.

Page 11: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.3 TYPE CAST (cont.)

System need to be informed- what kind of data is stored.

Void *x;

(int *)x;

It is called type-cast.

Script 10.2

Page 12: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

/* typecast.c - Script 10.2 */

#include <iostream.h>

#include <stdlib.h>

int main (void) {

/* Define and initialize a void pointer variable. */

void *x = NULL;

/* Request a block of memory large

* enough to hold an integer. */

x = malloc(sizeof(int));

/* We have omitted the check for NULL

* here. You must always check for a NULL

* return value from malloc(). */

/* Print out the starting address of the memory block. */

Page 13: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

cout<<"Starting address: “<< x<<endl;/* Store an integer in the new memory block. */*(int *)x = 1234;/* Print the value. */cout<<"Value stored in the memory block: “<< *(int*)x);/* Return the block to the pool. */free(x);x = NULL;/* Pause and wait for input before terminating. */getchar();return 0;}

Page 14: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.4 ALLOCATING ARRAYS OF DYNAMIC SIZE

The example discussed in previous still consider static memory.

The amount of should be determine and request during runtime.

Refer to script 10.3

Page 15: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

/* array.c - Script 10.3 */#include <iostream.h>#include <stdlib.h>int main (void) {

/* Define an integer pointer variable. */int *x = NULL;

/* Define two integer variables. */int i, count;

/* Prompt for the number of items. Store * the user's reply in the count variable. */cout<<"Number of items? ";cin>>count;

Page 16: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

/* Request a block of memory large * enough to hold the requested number * of integers. */x = (int*)malloc(count * sizeof(int));

/* We have omitted the check for NULL * here. You must always check for a NULL * return value from malloc(). *//* Store a random number in every slot * of the array. */for (i = 0; i < count; i++) {

x[i] = rand();}

Page 17: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

/* Print all values in the array. */for (i = 0; i < count; i++) {cout<<“The value of array element “<< i <<“ is “<<x[i] <<endl;}

/* Return the block to the pool. */free(x);x = NULL;/* Pause and wait for input before terminating. */

return 0;}

Page 18: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.5 Resizing a Block of Memory

Sometime you have to resize a block of dynamic memory after you have already stored data in it.

The C++ library provides the realloc() function for this purpose.

void *x *y;

x=malloc(1000); // initial block

Y= realloc(x,2000);// doubled

Page 19: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.5 Resizing a Block of Memory(2)

realloc() attempts to grow the original size to accommodate the new requested size.

If successful:- it returns the block’s unchanged.If it not possible to grow the old block (not

enough adjacent free space in memory):- realloc() will instead allocate a new block in a different part of memory; copy all over of the old data to the new block and return the address of that new block.

Page 20: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

2.5 Resizing a Block of Memory(3)

Upon failure, the realloc() returns the NULL pointer instead.

Script 10.4

Page 21: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

Stop! Do exercise

This code is bad, why?void *x ;

x=malloc(1000); // initial block

x= realloc(x,2000);// doubled

If realloc() fails and return a NULL pointer, the initials address of the memory block returned by the malloc(1000) call is lost. Since that value can longer be passed to free(), the originally reserved memory cannot be reclaimed until the program terminates. This is called memory leak.

Page 22: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

Summary

Dynamic memory allow you to create and use data structure that can grow and shrink.

malloc() function is used to request a block of certain size (in byte).

free() function is used to return it (memory) to the pool.

Page 23: Managing Memory DCT 1063 PROGRAMMING 2 Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College nazri@tatiuc.edu.my

Summary(2)

realloc adjusts the size of the allocated block to size, copying the contents to a new location if necessary.