wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

Tutorial

Javier Sanchez

How much memory do we need to allocate dynamically and how do we do it?

                                                                                

Brief Introduction to Dynamic Memory Allocation

As the book says Dynamic memory allocation usually requires two main structures: data objects and pointers.  I can also describe this type of allocation as just being able to share memory with other programs that are not running in the background and that dynamic memory must be allocated at runtime. Up to this time we have been requesting the use of RAM on a static basis. Static memory means we reserve a certain amount of memory when we write the programs. When we do that then we have reserved that particular memory and no other program can use it, even if we are not using it at the same time as the other programs.


Now that we have a basic introduction to what Dynamic Memory Allocation is, we can now move on and start discussing how much memory we need to allocate dynamically and how do we do it. 

Determining how much RAM we need depends on the size of the records that we are creating. If we are constructing a record then all of the bytes of memory needed for the record should be contiguous not dispersed around.  With the use of Dynamic Memory Allocation we can go ahead and remove the constraint because Dynamic memory allocation now lets us us non contiguous bytes of memory is very helpful in cases.

Lets explain what we mean by contiguous. When we mean contiguous bytes of memory and not dispersed we mean that the bytes in memory have to be enough to accommodate our program in memory and also they have to be next to each other.

 

 

 

 

 

 

Lets say that the record that we are constructing is going to use 80kb contiguous bytes of RAM, that means that according to the memory chart above there are no contagious bytes of RAM available.  The biggest is 74kb but still is less than what we need.   If you could run the program like that with no memory available you might get the NOT ENOUGH MEMORY message.

Now lets says that our program only needs 49 kb of contiguous RAM then it would fit in 51 kb available here.


Now in order to be able to determine the exact amount of RAM and to get those available addresses that we need to run the program that we are constructing there are a very useful operators to do this job. These operators are malloc and sizeof and they are found in the stdlib.h file.

For example lets say that we are writing a program to create a record for 4 employees we would right some code like this:

struct emplorec
{   char      ssn[10];10-bytes
     char      name[25]; };       25-bytes

For this record we would need approx. (35*4(employees)) 140 bytes of contiguous RAM.

If the programmer wants to check if memory will be available he can use the following statement to check for that.                                                                                                             

newemplo = (struct emplotrec *) malloc(sizeof(struct emplorec));              This declaration  means that you are requesting the needed memory

 

                                  The following code is what follows the code above and is used to check for memory

                if (newemplo == NULL) // would there be enough memory for the program to run?

                { puts("Memory allocation failed. try again");

                    return(0); }

                                                                                                                                               

                                                                                                               

Multiple Choice Questions

1. In Dynamic Ram Allocation there are two very important operators to determine the memory needed for us to run our programs, they are:

a) storage disk operators              d) malloc operator

b) Dynamic operators                   e) both c & d

c) sizeof operator                     f) None these are correct

Answer

2. There are two main structures in dynamic memory allocation, what are they:

a) integers & floats

b) RAM & Storage

c) pointers & objects

d) none of these

Answer

Short Answer Questions

1. What is the difference between static and dynamic memory allocation?

 

2. Why do we sometimes get the NOT ENOUGH MEMORY message?

Answers

Following are some very good references on allocating memory dynamically.

http://www.technoplaza.net/programming/index.cgi?p=lesson6

http://calc.utep.edu/pkirs/3355/lecslide.htm  (click on the link for this topic to download pp slides)

http://howstuffworks.lycoszone.com/ram4.htm

 

 

 

 

Answers to questions

Multiple Choice

1. e

2. c

Short Answers

1. The difference is that static memory is  when we reserve a certain amount of memory when we write the programs and in dynamic we can use some methods to know how much memory we need to run the program; dynamic is also determined at run time.

2. We get that message because you sometimes don't have enough contiguous bytes of RAM to run the program.