malloc()

Paul A. Varner pvarner at blackbird.afit.af.mil
Sun Apr 8 09:20:27 AEST 1990


In article <1550 at blackbird.afit.af.mil> I wrote:
>To all you 'C' gurus:
>  I am in the process of porting some C code to be as portable as
>possible across many different machines.  Anyhow, one of the calls I
>have to make is malloc(something), where somthing is a BIG value, such
>as 300,000.  Anyhow, the code works perfectly on a Unix machine I have
>access to, but bombs out in Turbo C, using the huge memory model.
>When I say bomb, it locks the machine up.  Anyhow, I really need to
>keep the Malloc call in the code.  Any suggestions??

In response to this, I have received many suggestions.  I appreciate all
the help and thank the following people who sent me their advice.

rds95 at leah.Albany.EDU (Robert Seals)
raymond at math.berkeley.edu (Raymond Chen)
D. Richard Hipp <drh at cs.duke.edu>
tarvaine at jyu.fi (Tapani Tarvainen)
grimlok at hubcap.clemson.edu (Mike Percy)
einari at rhi.hi.is (Einar Indridason)
Alvin I Rosenthal <air at unix.cis.pitt.edu>
kdq at demott.com (Kevin D. Quitt)
"Rich Walters" <raw at math.arizona.edu>
Marvin Rubenstein <marv at ism.isc.com>
gordon at sneaky.lonestar.org (Gordon Burditt)
G.Toal%edinburgh.ac.uk at NSFnet-Relay.AC.UK
HENRIK SANDELL <E89HSE at rigel.efd.lth.se>
npw at nbsr.nbsr.duke.edu (Nicholas Wilt)
s.michnowicz at trl.oz.au (Simon Michnowicz - A Free Spirit)
kim at wacsvax.OZ (Kim Shearer)

My problem is caused by several things.  The first was that malloc is
defined so that it takes parameters of size_t. On the Unix system I was using
size_t is the length of a long.  In Turbo C, it is the length of an int.
Therefore, I was mallocing something like 300,000 mod 65536 and expecting to
have all 300,000 bytes.  The second was that since, I was actually allocating
some memory, the malloc call was not returning NULL.  The solution to this
problem was to use the function farmalloc.  This function takes a long as a
parameter.  The next thing is that in order to address all of the memory
allocated I needed to cast to a huge pointer.



More information about the Comp.lang.c mailing list