Turbo C large character array

Walter Bright bright at Data-IO.COM
Thu Aug 2 04:39:07 AEST 1990


In article <332 at sun13.scri.fsu.edu> mayne at VSSERV.SCRI.FSU.EDU (William (Bill) Mayne) writes:
<In article <1990Jul30.204053.28769 at ux1.cso.uiuc.edu> gordon at osiris.cso.uiuc.edu (John Gordon) writes:
<<The final solution:
<<	char huge menu[1200];
<<	for(i = 0; i < 1200; i++)
<<		menu[i] = farmalloc(80);

Neither the huge nor the farmalloc are necessary. Try this:
	char *menu[1200];
	for(i = 0; i < 1200; i++)
		menu[i] = (char *) malloc(80);

<There must  be a better way than using 1200 separate calls to
<malloc or farmalloc! In addition to the time
<you should be aware that in most implementations each malloc
<incurs memory overhead in addition to the storage requested.
<The minimum overhead on a PC is usually 16 bytes.

I don't know about other compilers, but for Zortech the overhead for malloc
is 2 bytes. For farmalloc, the overhead is whatever the DOS overhead is
for allocating segments.

<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);

Note that on a PC with 16 bit ints, 1200*80 == 30464 (!). This program
is going to crash horribly. You need to malloc a series of chunks, the
size of each is some multiple of 80 and less than 64k.

[Inadvertent overflowing of 16 bit ints in intermediate values is a
very common source of bugs in PC programs. I see a lot of it, and make
the same mistakes myself.]



More information about the Comp.lang.c mailing list