Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!tut.cis.ohio-state.edu!mailrus!purdue!haven!mimsy!mojo!russotto From: russotto@eng.umd.edu (Matthew T. Russotto) Newsgroups: comp.sys.mac.programmer Subject: Re: Novice Programmer Question: Dynamic Memory Allocation under TC Message-ID: <1989Dec16.185840.7649@eng.umd.edu> Date: 16 Dec 89 18:58:40 GMT References: <3427@husc6.harvard.edu> <24770@cup.portal.com> <4196@sbcs.sunysb.edu> <1856@neoucom.UUCP> <4265@sbcs.sunysb.edu> Sender: news@eng.umd.edu (The News System) Reply-To: russotto@eng.umd.edu (Matthew T. Russotto) Distribution: usa Organization: Merriversity of Uniland, College Purgatory Lines: 46 In article <4265@sbcs.sunysb.edu> vallon@sboslab10.cs.sunysb.edu (Justin Vallon) writes: >In article <3427@husc6.harvard.edu>, siegel@endor.harvard.edu (Rich >Siegel) writes: >> This is a common problem when porting C code from systems where >> sizeof(int) == sizeof(char *), because standard C assumes that a function >> which is called before it is prototyped (or before its definition is >> encountered) returns an int as its result. >> ... >> If you don't wish to #include , you can insert a line like >> >> char *malloc(); > >Isn't the problem also that malloc also accepts a long size? When I >started programming in C using toolbox calls, the biggest problem that >I had was forgetting to #include , and Think C would assume >that Handle NewHandle(long) was actually int NewHandle(int). The int >would be passed, but NewHandle needed a long, it took my two bytes, and >another extra two from just above the parameter, and I kept getting >out of memory errors (when allocating 20 bytes), and I'd get a NULL >handle. unsigned short i; for (i=0; i < 65534; i++) { printf("Without prototypes, C does no type checking on its parameters\n"); } This applies to all C routines, as well as non-inline ROM calls (implemented in MacTraps). The following WILL work: Handle NewHandle(); main() { DisposHandle(NewHandle(20L)); } In fact, for the sick and warped, the following will allocate and dispose of a 20 byte handle also: Handle NewHandle(); main() { Point p; SetPt(&p, 20, 0); DisposHandle(NewHandle(p)); } A prototype is not necessary to make these routines work properly. What is necessary is careful attention to the length of the parameter passed. -- Matthew T. Russotto russotto@eng.umd.edu russotto@wam.umd.edu ][, ][+, ///, ///+, //e, //c, IIGS, //c+ --- Any questions?