/* File: allocate2.c

   This file demonstrates memory allocation.
*/

#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

/* On some systems you should include the following */
#include <malloc.h>

main() {
   int i, *A, elements ;
   int power_of_2 ;


   printf("Enter number of elements in integer array: ") ;
   elements = GetInteger() ;

   /* get a block of memory for size integer */
   A = (int *) malloc( elements * sizeof(int) ) ;
   if (A == NULL) {
      Error("Oops, we're out of memory\n") ;
   }

   /* Lets use A as an array */
   power_of_2 = 1 ;
   for (i = 0 ; i < elements ; i++) {
      A[i] = power_of_2 ;
      power_of_2 = 2 * power_of_2 ;
   }

   /* Print out contents of the array */
   for (i = 0 ; i < elements ; i++) {
      printf("A[%d] = %d\n", i, A[i]) ;
   }

   /* Give up use of the memory block */
   free(A) ;
}
