Previous: 14.2 Dynamic Memory Allocation
Up: 14.2 Dynamic Memory Allocation
Next: 14.2.2 Dynamic Arrays
Previous Page: 14.2 Dynamic Memory Allocation
Next Page: 14.2.2 Dynamic Arrays

14.2.1 Library Functions for Dynamic Allocation

Two standard library functions are available for dynamic allocation. The function malloc() allocates memory dynamically, and the function free() deallocates the memory previously allocated by malloc(). When allocating memory, malloc() returns a pointer which is just a byte address. As such, it does not point to an object of a specific type. A pointer type that does not point to a specific data type is said to point to void type, i.e. the pointer is of type void *. In order to use the memory to access a particular type of object, the void pointer must be cast to an appropriate pointer type. Here are the descriptions for malloc(), and free():

If successful, malloc() returns a pointer to the block of memory allocated. Otherwise, it returns a NULL pointer. One must always check to see if the pointer returned is NULL. If malloc() is successful, objects in dynamically allocated memory can be accessed indirectly by dereferencing the pointer, appropriately cast to the type of pointer required.

The size of the memory to be allocated must be specified, in bytes, as an argument to malloc(). Since the memory required for different objects is implementation dependent, the best way to specify the size is to use the sizeof operator. Recall that the sizeof operator returns the size, in bytes, of the operand.

For example, if the program requires memory allocation for an integer, then the size argument to malloc() would be sizeof(int). However, in order for the pointer to access an integer object, the pointer returned by malloc() must be cast to an int *. The code takes the following form:

int *ptr;
     ptr = (int *)malloc(sizeof(int));
Now, if the pointer returned by malloc() is not NULL, we can make use of it to access the memory indirectly. For example:
if (ptr != NULL)
          *ptr = 23;
Or, simply,
if (ptr)
          *ptr = 23;
     printf("Value stored is %d\n", *ptr);

Later, memory allocated above may no longer be needed. In which case, it is important to free the memory. Thus:

free((void *) ptr);
deallocates the previously allocated block of memory pointed to by ptr. Or, more simply, we could write:
free(ptr);
ptr is first converted to void * in accordance with the function prototype, and then the block of memory pointed to by ptr is freed.

It is possible to allocate a block of memory for several elements of the same type by giving the appropriate value as an argument. Suppose, we wish to allocate memory for 100 float numbers. Then, if fptr is a float *, the following statement does the job:

fptr = (float *) malloc(100 * sizeof(float));
Pointer fptr points to the beginning of the memory block allocated, i.e. to the first object of the block of 100 float objects, fptr + 1 points to the next float object, and so on. In other words, we have a pointer to an array of float type. The above approach can be used with data of any type including structures. The example in Figure 14.14 allocates memory for a structure, reads data into it, and then prints the data.

Sample Session:



Previous: 14.2 Dynamic Memory Allocation
Up: 14.2 Dynamic Memory Allocation
Next: 14.2.2 Dynamic Arrays
Previous Page: 14.2 Dynamic Memory Allocation
Next Page: 14.2.2 Dynamic Arrays

tep@wiliki.eng.hawaii.edu
Sat Sep 3 07:21:51 HST 1994