Turbo C large character array

William Bill Mayne mayne at VSSERV.SCRI.FSU.EDU
Tue Jul 31 09:44:11 AEST 1990


In article <1990Jul30.204053.28769 at ux1.cso.uiuc.edu> gordon at osiris.cso.uiuc.edu (John Gordon) writes:
>
>	Well, I managed to solve my problem, with thanks to all who posted and 
>e-mailed me suggestions.  The final solution:
>
>	char huge menu[1200];
>
>	for(i = 0; i < 1200; i++)
>		menu[i] = farmalloc(80);
>

Don't be so hasty about the "final solution." 
There must  be a better way than using 1200 separate calls to
malloc or farmalloc! In addition to the time (which admittedly
may not be too much of a concern since you only do this once)
you should be aware that in most implementations each malloc
incurs memory overhead in addition to the storage requested.
The system must keep track of all those separate allocations
so that when you do the frees later he knows the associated
length. The minimum overhead on a PC (which I assume you 
are using from your description) is usually 16 bytes. You'd do 
better to allocate a big block and set your own pointers to the
individual elements. Something like this:

char *hugeblock, *block[1200];
hugeblock=malloc(1200*80);
for (i=0; i<1200; ++i)
  block[i]=hugeblock+80*i;
/* rest of your code goes here */
free(hugeblock);

This assumes the huge memory model, but with some slight variation
you could get by with large, allocating the amount you need in
pieces <64K but setting the pointers in block as one array.
Also, this is not quite as efficient as it could be because
I wanted it to be as clear as possible without long explanations.
But I think you can get the idea from this.



More information about the Comp.lang.c mailing list