Large 2D array woes...

marwk at levels.sait.edu.au marwk at levels.sait.edu.au
Thu Mar 14 22:53:23 AEST 1991


In article <!+%=PZ$@rpi.edu>, fechuny at aix01.aix.rpi.edu (felix chung yau) writes:
>
>     I am a new programer in C and was wondering if someone could help me with
> a problem I have in using a two dimensional array.  I am currently using
> Microsoft Quick C and my problem is in dimensioning an integer array like
> test[400][400].  When I compile the program I get the message "allocation
> exceeds 64k".  I have tried using the "far" keyword to define the variable
> as a far pointer but that does not seem to work.  I was wondering if there
> is a better way to define and handle large arrays.
>     Thank you in advance.

In TURBOC one has to use huge pointers aas in the following example.
This means that a 32-bit pointer is used to point to the data.  FAR pointers
do not work as they still wrap around at the 64K mark.

#include <stdio.h>
#include <alloc.h>

typedef char huge *   MYLARGE;

long i;
MYLARGE lots;

#define SIZE 65538L

void main(void)
    {
    lots = (MYLARGE) farmalloc(SIZE);

    for (i = 0; i < SIZE; ++i)
        lots[i] = i % 10L + 1;

    for (i = 0; i < 10L; ++i)
        printf("%ld: %2d\n", i, lots[i]);
    }


for a 400 * 400 array this is not necessary (just realised this, sorry):

char far * p_rows[400];

for (i = 0; i < 400; ++i)
    p_rows[i] = (char far *) malloc(400 * sizeof(char));

This allocates 400 columns for each row.

FAR pointers are required as there is not enough room on the near heap
to hold all the data.

Now the element in the r'th row and c'th column is p_rows[r][c], just as
you would expect.

Ray
--
University of South Australia   | Plus ca change, plus c'est la meme chose.
P.O. Box 1                      | Ghing thien me how, ming thien gung me how.
Ingle Farm                      | Knobs, knobs everywhere,
South Australia                 |              just vary a knob to think!



More information about the Comp.lang.c mailing list