Lecture 6: Dynamic memory allocation

mangledcobwebΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

70 εμφανίσεις

+

Dynamic memory
allocation

+

Introduction


We often face situations in programming where the data is
dynamics in nature.


Consider a list of customers. When list grows we need to
allocate more memory space to the list to accommodate
additional data items.


Such situation can be handled by using dynamic memory
management.

+

Dynamics memory allocation


C language requires the number of elements in an array to
be specified at compile time. But if we wrong with initial
guess of number of elements?


It may cause failure of the program (not enough elements)


It can was memory space (to many unused elements)


The solution is to allocate memory at run time (dynamic
memory allocation).


C does not have such functionality, but there four library
routines that allows to allocate memory dynamically.

Function

Task

malloc

Allocate request size of

bytes and return a pointer to the first byte

calloc

Same as
malloc

but initialize memory to zero

free

Frees previously allocated

space

realloc

Modifies the sizes of previously

allocated space

+

Memory allocation process


Memory allocation process associated with a C program


The program instruction, global and static variables are
stored in
permanent storage area
.


Local variables are stored in
stack
.


The free memory is called the heap. The size is changing, it is
possible to encounter memory “overflow” during dynamic
allocation process.

Local

variables

Free memory

Global

variables

C program instruction

stack

heap

p
ermanent
storage area

+

Allocating a block of memory:
malloc


The
malloc

function reserves a block of memory of specified
size and return a pointer of type
void
.



ptr

= (cast
-
type *)
malloc
(byte
-
size);



x = (
int

*)
malloc
(100 *
sizeof
(
int
));



cptr

= (
char

*)
malloc
(10);

On successful execution, a memory space will be allocated.



cptr

Address of
first byte

10 bytes of
space


The
malloc

allocates a block of contiguous bytes.

+

Example


The program uses a table of integers whose size will be
specified interactively at run time.

+

Allocating multiple blocks of memory:
calloc


c
alloc

allocates multiple blocks of storage, each of the same
size, and then sets all bytes to zero.


The general form of
calloc

is

p
tr

= (cast
-
type *)
calloc

(n,
elem
-
size);


If there is not enough space, a NULL pointer is returned.

+

Releasing the used space: free


Compile
-
time storage of a variable is allocated and released
by the system in accordance with its storage class.


With the dynamics run
-
time allocation, it is our responsibility
to release the space when it is not required. The release of
storage space becomes important when the storage is
limited.


When we no longer need the data we stored in a block
memory, we may release that block of memory for future use


free

free

free

free

free

occupied

free

free

free

free

free

free

free

free

free

free

free

free

free(
ptr
);

before

after

+

Altering the size of a block:
realloc


It likely that we discover later:


The previously allocated memory is not sufficient (add more space)


The memory allocated is much larger that necessary (reduce space)


We can changed allocated memory with the function
realloc
.



ptr

=
malloc
(
size
);



ptr

=
realloc
(
ptr
,
newsize
);

The
newsize

maybe large or smaller than the
size
.

+

Example


The program stores a character string in a block of memory
space created by
malloc

and then modify the same to store a
larger string.

+

Dynamically allocating
Multidimensional arrays


it's straightforward to call
malloc

to allocate a block of memory
which can simulate an array, but with a size which we get to pick
at run
-
time.


Can we do the same sort of thing to simulate multidimensional
arrays?


W
e
want to simulate an array of pointers, but we don't know how
many rows there will be, either, so we'll have to simulate that
array (of pointers) with another pointer, and this will be a pointer
to a pointer.

+

Example

+

Dynamically allocating
Multidimensional arrays


I
f

a

program

uses

simulated,

dynamically

allocated

multidimensional

arrays,

it

becomes

possible

to

write

``heterogeneous''

functions

which

don't

have

to

know

(at

compile

time)

how

big

the

``arrays''

are
.

O
ne

function

can

operate

on

``arrays''

of

various

sizes

and

shapes
.





func
(
int

**array,
int

nrows
,
int

ncolumns
){}


Example




To
free one of these dynamically allocated multidimensional ``arrays,''
we must remember to free each of the chunks of memory that we've
allocated