Software and s/w Development

Dec 14, 2013 (4 years and 5 months ago)

86 views

Page
1

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Dynamic Memory Allocation

Suppose we defined the data type:

struct custrec

{ char

ssn[10];

char

name[31]; };

And then we were to make the declaration:

struct custrecrec customer[1000];

(Which requires 41
-
bytes of contiguous RAM)

(Which requires 41 * 1,000 = 41,000 bytes of contiguous RAM)

We might get the run
-
time error message:

Not enough memory

(Or something similar)

Page
2

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Well, That’s because there is not enough memory, Right???

Not necessarily.

If we were to check, we might find that we actually have 250,000
bytes of ram available.

Why Can’t we get the memory we need ??

If we were to look inside RAM, we might see:

401KB: Assigned

36 KB Available

43 KB: Assigned

28 KB:
Avail.

78 KB: Assigned

22 KB: Avail.

234 KB: Assigned

29 KB: Available

512 KB: Assigned

35 KB: Avail.

386 KB: Assigned

31 KB: Avail.

12 KB: Avail.

18 KB: Avail.

26 KB: Avail.

127 KB: Assigned

14 KB: Avail.

23 KB: Assigned

30 KB: Avail.

32 KB: Assigned

76 KB: Assigned

Page
3

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

To get the necessary memory we need
NON
-
CONTIGUOUS

storage.

Let’s redefine our data type:

struct custrec

{ char

ssn[10];

char

name[31];

struct custrec * next; };

How can we have a pointer within a structured object ??

Remember, when we defined a structured data object (
struct
),

we noted that it could also include a pointer:

• The variable (location in RAM)
next

(assume
next

65100
),

we will find (on 45
-
bytes):

(Which requires 45
-
bytes of contiguous RAM)

65100
-

65109

ssn

65110
-

65140

name

65141
-

65145

next

Character array
on 10
-
bytes

Character array
on 31
-
bytes

4
-
bytes

Page
4

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

How would this work ???

Assume that we wished to keep track of the following records:

ssn

name

‘123456789’

‘234567890’

‘345678901’

‘456789012’

‘567890123’

‘Chaucer, G.’

‘Milton, J.’

‘Browning, R.’

‘Eliot, T.S.’

‘Shelley, P.’

But if we were to look at available RAM we saw:

Assigned

9200
-

9253

Available

9254
-

9302

Assigned

9303
-

9321

Available

9322
-

9417

Assigned

9404
-

9543

Available

9544
-

9598

Assigned

9599
-

9621

Available

9622
-

9774

Page
5

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

We know that our structured data object requires:

struct custrec

{ char

ssn[10];

10
-
bytes

char

name[31]; };

31
-
bytes

41
-
bytes

of contiguous storage

OR
: 41 * 5 =
205
-
bytes

of contiguous storage for the database

IF we look at our available storage, however:

Available Locations

9254
-

9302

Contiguous RAM Available

49

9322
-

9417

96

9544
-

9598

55

9622
-

9774

153

353 Total Bytes

Which is enough for our entire database, and even 1, 2, or 3

records contiguously,
BUT NOT ALL 5 RECORDS

CONTIGUOUSLY

Page
6

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Now, let’s redefine our data type as we did before:

struct custrec

{ char

ssn[10];

10
-
bytes

char

name[31];

31
-
bytes

struct custrec * next; };

4
-
bytes

45 bytes

of contiguous storage

We could set
-
-
list to take advantage of our limitations:

“123456789”

9254
-

9263

“Chaucer, G.”

9264
-

9294

9295
-

9298

9322

“234567890”

9322
-

9331

“Milton, J.”

9332
-

9360

9361
-

9364

9365

“345678901”

9365
-

9374

“Browning R.”

9375
-

9405

9406
-

9409

9544

“456789012”

9544
-

9553

“Eliot, T.S.”

9554
-

9584

9585
-

9588

9622

“567890123”

9622
-

9631

“Shelley, P.”

9632
-

9662

9663
-

9666

NULL

Page
7

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

NOTE:

1.

We still need to allocate 45
-
contiguous bytes (the size of our record).

2.

Records may be allocated contiguously (as with records 3 and 4) but
need not be. If contiguous bytes are available, they will be allocated.
If not, the next (available) grouping of 45
-
bytes will be allocated.

3.

The space will be Allocated at RUN
-
TIME; NOT before

Does that mean that we must request 45
-
bytes of contiguous
storage 5 times (once for each record) ???

YES !

How do we request the needed memory ??

In C, the necessary statement is:

newcust = (struct custrec *) malloc(sizeof(struct custrec));

struct custrec * newcust;

Page
8

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Command Explanation:

struct custrec * newcust;

Sets aside 4
-
bytes of storage at location
newcust
.

newcust = (struct custrec *) malloc(sizeof(struct custrec));

Return the number of bytes we need (in our case: 45)

newcust = (struct custrec *) malloc(sizeof(struct custrec));

Function
malloc

(found in
stdlib.h
) takes one argument (the

number of contiguous bytes of RAM we need) and returns the

the address of where those contiguous bytes can be found.

Page
9

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

newcust = (struct custrec *) malloc(sizeof(struct custrec));

REMEMBER
: When we refer to an address, we MUST know

what type of data is stored there (in our case, the data type
struct

custrec
).

• We need to specify the data type which will be found at that

newcust = (struct custrec *) malloc(sizeof(struct custrec));

Store the address at which 45
-
bytes of contiguous (where the data

type
struct custrec

can be found) at location (variable)
newcust
.

What if there is NOT enough contiguous RAM available ??

Page
10

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

A good programmer will always check:

newcust = (struct custrec *) malloc(sizeof(struct custrec));

if (newcust == NULL)

//
is there enough memory?

{ puts("Memory allocation failed
-

Bye!");

return(0); }

Function
malloc

will return the value NULL if the requested
amount of contiguous RAM could not be found

What about the C code necessary ??

The code that follows assumes that:

1. The database will be entered from the keyboard

2. The User will continue entering records until they enter a
RETURN for the customer’s name

Page
11

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

struct custrec { char ssn[10], name[31]; struct custrec * next; };

int main()

{ struct custrec *newcust, *first, *previous, *present;

int numrecs = 0;

do

{ newcust = (struct custrec *) malloc(sizeof(struct custrec));

if (newcust == NULL)

{ puts("memory allocation failed
-

bye!");

return 0; }

if (numrecs == 0) first = newcust;

else previous
-
>next = newcust;

newcust
-
>next = NULL;

numrecs++;

gets(newcust
-
>name);

if (strlen(newcust
-
>name)>0)

{ printf("Enter SSN for customer %s: ",newcust
-
>name);

gets(newcust
-
>ssn); }

previous = newcust;

} while (strlen(newcust
-
>name)>0);

return 0; }

Page
12

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

There is one additional feature to dynamically allocated lists:

We can free
-
up memory which we no longer require

Assume we had set up our list (as before):

“123456789”

9254
-

9263

“Chaucer, G.”

9264
-

9294

9295
-

9298

9322

“234567890”

9322
-

9331

“Milton, J.”

9332
-

9360

9361
-

9364

9365

“345678901”

9365
-

9374

“Browning R.”

9375
-

9405

9406
-

9409

9544

“456789012”

9544
-

9553

“Eliot, T.S.”

9554
-

9584

9585
-

9588

9622

“567890123”

9622
-

9631

“Shelley, P.”

9632
-

9662

9663
-

9666

NULL

And we now wished to eliminate Eliot from the list

Page
13

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

How??

The procedure is relatively simple:

1.

Get the address of the record to be deleted (assume we store the

newcust
).

2.

Rearrange the pointers:

a.

If the first record, make the next record the first record

b.

If the last record, make the previous record the last one

c.

Otherwise, have the previous record point to the next record

3.

Free the address stored (at location
newcust
).

The C command necessary ???

location
newcust
, all we need to do is issue the command:

free(newcust);

Page
14

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

How is this different than re
-
arranging the pointers as we did

Unlike our previous examples, the space allocated for the record

(in our example, locations
9544

to
9588
) are now available for

reuse.

Page
15

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Page
16

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Page
17

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Page
18

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Page
19

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation

Page
20

Data Structures in C for Non
-
Computer Science Majors

Kirs and Pflughoeft

Dynamic Memory Allocation