Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!think.com!mintaka!bloom-beacon!eru!hagbard!sunic!mcsun!hp4nl!utrcu1!optimiza From: optimiza@utrcu1.UUCP (Streng) Newsgroups: comp.sys.atari.st.tech Subject: Re: Problem with my C program [a few general hints about malloc] Message-ID: <827@utrcu1.UUCP> Date: 22 Apr 91 19:49:43 GMT References: <1991Apr21.210056.5685@ccu.umanitoba.ca> <1991Apr22.014758.3846@wam.umd.edu> Reply-To: optimiza@utrcu1.UUCP (De Leeuw) Organization: Utwente, Enschede Lines: 31 In article <1991Apr22.014758.3846@wam.umd.edu> dmb@wam.umd.edu (David M. Baggett) writes: >If you want to allocate more memory than can be specified in an unsigned int, >use calloc. It takes two parameters which are multiplied together to get >the size of the requested block. To allocate 128K: > p = calloc(128, 1024); >As a bonus the memory you get is also cleared. You could write your own >replacement for Malloc as follows: >char * >my_Malloc(size) > register long size; >{ > register unsigned hi, lo; > hi = (unsigned) (size >> 16); > lo = (unsigned) (size & 0x0000FFFF); > return calloc(hi, lo); >} Unfortunately, this will not work the way you intend. Assume you want to Malloc() a block of 65536 bytes. This leads to hi being 1, and lo being 0. This will calloc() a block of 1*0=0 bytes. What you want to do is factorize size in two factors that are both small enough to be handled by calloc. I cannot come up right now with an algorithm that does this both time and memory efficient. But, there is another possibility. If you are using an ANSI-C compiler, the standard says that malloc takes an argument of type size_t, but not that it will be 16 bit. Turbo C, for example, takes a (maybe even unsigned) long. (unfortunately, this is not the case with MSDOS-TC, making ports somewhat more difficult) --- Henk de Leeuw optimiza@utwente.nl optimiza%utrcu1.uucp@uunet.uu.net