FREE
Shiping Zhang
ping at cubmol.bio.columbia.edu
Sat Mar 3 09:14:55 AEST 1990
In article <MEISSNER.90Mar2124443 at curley.osf.org> meissner at osf.org (Michael Meissner) writes:
>In article <1990Mar2.152601.8000 at cubmol.bio.columbia.edu>
>ping at cubmol.bio.columbia.edu (Shiping Zhang) writes:
>If you are allocating a very large area, and then set the area to all
>0's (or whatever bit pattern you want) on a virtual memory system, it
>will force each of the pages to be faulted in, which can be bad. For
>example, if you allocate a 10 meg chunk for the worst case senario,
>and only use say 256K, clearing the memory would force would touch
>9.75 meg of pages uselessly. A friend of mine sped up a linker on
>exactly this problem.
If I'm sure the worst case will need 10 meg and NO more and I want
allocate that much space all at once at the very begin, then I
will simply declare an ordinary variable of that size. If that
space will be used for different types of data, I can define an union
to deal with the problem. Otherwise, I will only malloc() 10 kb or
1 kb first, then later realloc() more space if necessary.
Working with arrays, the boundries should always be checked, so no
extra boaden is given to find out when realloc() should be called.
Because malloc(), calloc() and the like belong to one set of
functionally related routines, the way of using them should
be consistant. They should all either do initialization (with the
same results machine indepently) or all do not, but not some do,
some don't. If initialization can not be gaurenteed to give
consistent results machine indepently, then it is much much better
to just leave the programers no choice but doing initialization
themselves if necessary. Don't let programers debate in their
mind and take chances.
-ping
More information about the Comp.lang.c
mailing list