### How to Define Arrays in Turbo C using the Heap.

> > > Shown below is a program that uses the heap to define multiple large arrays. Most of the information you need to understand this program is available from the help menus (right mouse click) in Turbo C.

> > >     // Program that demonstrates the use of the heap //

> > >     // for storage of arrays.     fjlooft@ee.wpi.edu //

> > >     #include <conio.h>

> > >     #include <stdio.h>

> > >     #include <malloc.h>           // used by heap procedure call //

> > >     #include <math.h>

> > >     const RecordSize = 10000;     // has to be less than 32,766 //

> > >     float *X=NULL;                // define the pointers here //

> > >     int   *Y=NULL;

> > >     int    i, j, k, array_size;

> > >     char   ch;

> > >     float  a, b;

> > >     // ----------------- start of main program ---------------- //

> > >     void main()

> > >     { clrscr();

> > >       a = 3.14159/9.0;

> > >       array_size = RecordSize;

> > >       X     = calloc(sizeof(float), array_size);   // create the arrays  //

> > >       Y     = calloc(sizeof(int  ), array_size);   // one int, one float //

> > >       if( X==NULL || Y==NULL )                 // if no space, then exit //

> > >       { cprintf("\n\r ERROR - No Space Allocated. \n\r"); goto NoFile; }

> > >       textcolor( WHITE     ); cprintf("\n\n\r  Filling X...");

> > >       for (i=0; i<=array_size-1; i++)

> > >       { b    = 100.0*sin( a*i );

> > >         X[i] = b; }          // the array can be used in a normal manner //

> > >       for (i=0; i<=9; i++ ) cprintf("\n\r %6.2f", X[i] );

> > >       textcolor( LIGHTGRAY ); cprintf("\n\n\r  Transferring to Y...");

> > >       for (i=0; i<=array_size-1; i++) Y[i] = X[i];

> > >       for (i=0; i<=9; i++ ) cprintf("\n\r %3i", Y[i] );

> > >       textcolor( LIGHTCYAN );

> > >       cprintf("\n\r Done... \n\r");

> > >       NoFile: free( Y );

> > >               free( X );

> > >       exit(1);

> > >     }

> > F. Looft, WPI ECE Dept. fjlooft@ee.wpi.edu