(Peter Lim take note!) Declaring very large global array

Derek R. Foster dfoster at jarthur.Claremont.EDU
Sun Apr 15 07:51:17 AEST 1990


On declaring arrays like
  data[MAX_CHANNELS][MAX_SAMPLES]
with MAX_CHANNELS = 6, MAX_SAMPLES = 3000
(Which runs up against the old >64K array problem)

People, PLEASE! Realize there's a better way to do this!
You don't need to fill your programs with *(pointer+j)[i] 's! You can just
use pointer[i][j] 's!

Watch...

In article <15080006 at hpfinote.HP.COM> pnl at hpfinote.HP.COM (Peter Lim) writes:
>You need to play with pointers. In your case, why don't just declare
>an array of 6 pointers, each is then used to index 3000 samples. Like:
>
>float (*dptr)[MAX_CHANNELS];
>
>for (i=0; i < MAX_CHANNELS; i++)
>   (*dptr)[i] = (float *) malloc ((unsigned) (MAX_SAMPLES * sizeof (float)));
>
>then proceed to use
>   (*(dptr+j))[i] ..... in place of data[i][j].

There is a MUCH nicer way. Just do this:

float * data[MAX_CHANNELS];

for (i=0; i<MAX_CHANNELS; i++)
  data[i] = (float *) malloc (MAX_SAMPLES * sizeof(float));

then proceed to use
   data[i][j] ..... in place of data[i][j] ! :-)

You can use the EXACT SAME CODE FROM THIS POINT ON! YOU DON'T NEED TO
GO BACK AND EDIT EVERY ARRAY ACCESS!

>Regards,                       ## Life is fast enough as it is ........
>Peter Lim.                     ## .... DON'T PUSH IT !!          >>>-------,
>                               ########################################### :
>E-mail:  plim at hpsgwg.HP.COM     Snail-mail:  Hewlett Packard Singapore,    :
>Tel:     (065)-279-2289                      (ICDS, ICS)                   |
>Telnet:        520-2289                      1150 Depot Road,           __\@/__
>  ... also at: pnl at hpfipnl.HP.COM            Singapore   0410.           SPLAT !

Derek Riippa Foster



More information about the Comp.lang.c mailing list