UMBC CS 201, Spring 02
UMBC CMSC 201 Spring '02 CSEE | 201 | 201 S'02 | lectures | news | help

A better malloc() example

In this example, we want to have an array called values known in main(), and we would like to use dynamic memory allocation to get the memory needed to hold the elements of the array. Further, we would like to write a function called GetMemory that will do the dynamic allocation to get that code out of main().

Notice that we declare a int* called values in main(), but it holds garbage originally. We would like the function GetMemory to modify the variable values to hold the beginning address of the block of memory that is being dynamically allocated within the function. Since we would like the function to modify this variable, we will have to pass the address of values to the function, so that the function can modify it by derefencing that pointer.

Remember that when we wanted to modify an integer variable, we passed the address of that variable into a function and the type of the function's parameter that accepts it is int*, or a pointer to an int.

Similarly if we want to modify a variable which is a pointer to an int, we will pass the address of the variable into the function and the type of the function's parameter that accepts it will be int**, or a pointer to a pointer to int.

/********************************* ** File malloc2.c ** Author: S. Bogar ** Date: 1/3/95 ** Section: 101 ** SSN: 123-45-6789 ** E-Mail: bogar@cs.umbc.edu ** ** This file demonstrates passing a pointer ** by reference to a function ** A pointer to a pointer *******************************/ #include <stdio.h> #include <stdlib.h> /* function prototype */ void GetMemory (int count, int **array); int main() { int i, *values, nrElements ; int powerOf2 ; printf("Enter number of elements in integer values: ") ; scanf("%d", &nrElements) ; /* get a block of memory for 'nrElements' integers ** since 'values' is a pointer to int ** '&values' is the address of (pointer to) a pointer ** to int so that 'values' can be modified in the ** function */ GetMemory (nrElements, &values); /* Lets use the memory as an arrray */ powerOf2 = 1 ; for (i = 0 ; i < nrElements ; i++) { values[i] = powerOf2 ; powerOf2 = 2 * powerOf2 ; } /* Print out contents of the array */ for (i = 0 ; i < nrElements ; i++) { printf("values[%d] = %d\n", i, values[i]) ; } /* Give up use of the memory block */ free(values) ; return 0; } /*************** ** Function: GetMemory ** Inputs: Number of integers for which to allocate ** space ** Pointer to the pointer that will point to ** the memory ** Output: The pointer to the memory is set ** there is no return value ********************************/ void GetMemory (int count, int **array) { /* malloc the memory and set the caller's pointer ** to point to it */ *array = (int *) malloc( count * sizeof(int) ) ; if (*array == NULL) { printf("Oops, we're out of memory\n") ; exit(-1); } }

output

linux1[89] % a.out Enter number of elements in integer values: 10 values[0] = 1 values[1] = 2 values[2] = 4 values[3] = 8 values[4] = 16 values[5] = 32 values[6] = 64 values[7] = 128 values[8] = 256 values[9] = 512 linux1[90] % a.out Enter number of elements in integer values: 20 values[0] = 1 values[1] = 2 values[2] = 4 values[3] = 8 values[4] = 16 values[5] = 32 values[6] = 64 values[7] = 128 values[8] = 256 values[9] = 512 values[10] = 1024 values[11] = 2048 values[12] = 4096 values[13] = 8192 values[14] = 16384 values[15] = 32768 values[16] = 65536 values[17] = 131072 values[18] = 262144 values[19] = 524288 linux1[91] % a.out Enter number of elements in integer values: 88888888888888 Oops, we're out of memory linux1[92] %


CSEE | 201 | 201 S'02 | lectures | news | help

Last Modified - Thursday, 04-Apr-2002 15:03:57 EST