Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!asuvax!ukma!psuvax1!hsdndev!husc6!purdue!haven!wam.umd.edu!dmb From: dmb@wam.umd.edu (David M. Baggett) Newsgroups: comp.sys.atari.st.tech Subject: Re: Problem with my C program [a few general hints about malloc] Message-ID: <1991Apr22.014758.3846@wam.umd.edu> Date: 22 Apr 91 01:47:58 GMT References: <1991Apr21.210056.5685@ccu.umanitoba.ca> Sender: usenet@wam.umd.edu (USENET Posting) Organization: University of Maryland at College Park Lines: 49 In article <1991Apr21.210056.5685@ccu.umanitoba.ca> umochock@ccu.umanitoba.ca (Russell Ochocki) writes: > >I try to Malloc a buffer equal to the size of the file being copyied. Then I >do a Fread from the source file, prompt the user to swap disks, fopen the >destination file, and Fwrite the contents of the buffer. And, fclose both >files. Note: I needed to use Malloc, Fread, and Fwrite since I required long >ints (not the short expected by malloc, fread, and fwrite). Although this isn't what you were asking about, I think it's worth mentioning for the benefit of others who are just starting out: 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); } (This assumes that a long is 32 bits and an unsigned int is 16, which is not portable, so it's best to use calloc directly.) In general it is VERY WISE to avoid using the bios Malloc and Free and use the malloc, calloc, and free provided with your compiler of choice. The bios Malloc has bugs (some still remain for historical reasons) and is quite ideosyncratic. Sozobon's malloc and calloc (in dlibs.a) work quite well, although one caveat to Sozobon users is that the malloc routines can only manage 1 Meg of memory. A trivial change to the dlibs source file malloc.c fixes this, fortunately. Also, assuming you were using fread and fwrite, you would have to be sure to specify "b" in the flags ("b" for "binary") to prevent automatic conversion of CR to CR/LF. Dave Baggett dmb%wam.umd.edu@uunet.uu.net