🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac
Article #43991

Re: Novice Programmer Question: Dynamic Memory Allocation under TC

#43991
From: Justin Vallon
Date: Fri, 15 Dec 1989 17:10
42 lines
2383 bytes
In article <3427@husc6.harvard.edu>, siegel@endor.harvard.edu (Rich
Siegel) writes:
> In article <1856@neoucom.UUCP> sam@neoucom.UUCP (Scott A. Mason) writes:
> >Actually the fix may be very simple.  I had a similiar problem when porting
> >some code from an (yuck) MeSsy-DOS program.
> >I finally found out that Think C happily lets me compile a program with
> >(x)alloc calls in it without the library included, but does strange things
> >with the pointers allocated.
> 
> 	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 <storage.h>, 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 <MemoryMgr.h>, 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.

I don't know if the same problem applies to malloc, but I'd assume that
it probably does.  You should probably put char *malloc(long); as a
prototype.

I also found that "Require Prototypes" solves almost ALL of these type
problems.

> ~~~~~~~~~~~~~~~
>  Rich Siegel
>  Internet: siegel@endor.harvard.edu
>  UUCP: ..harvard!endor!siegel
> ~~~~~~~~~~~~~~~

-Justin
vallon@sbcs.sunysb.edu
Technical Details
Message-ID: <4265@sbcs.sunysb.edu>
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!psuvax1!brutus.cs.uiuc.edu!wuarchive!udel!sbcs!sboslab10!vallon
References: <3427@husc6.harvard.edu> <24770@cup.portal.com> <4196@sbcs.sunysb.edu> <1856@neoucom.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!psuvax1!brutus.cs.uiuc.edu!wuarchive!udel!sbcs!sboslab10!vallon
From: vallon@sboslab10.cs.sunysb.edu (Justin Vallon)
Newsgroups: comp.sys.mac,comp.sys.mac.programmer
Subject: Re: Novice Programmer Question:  Dynamic Memory Allocation under TC
Message-ID: <4265@sbcs.sunysb.edu>
Date: 15 Dec 89 17:10:17 GMT
References: <3427@husc6.harvard.edu> <24770@cup.portal.com> <4196@sbcs.sunysb.edu> <1856@neoucom.UUCP>
Sender: news@sbcs.sunysb.edu
Reply-To: vallon@sboslab10.cs.sunysb.edu (Justin Vallon)
Followup-To: comp.sys.mac
Distribution: usa
Lines: 42