Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ames!nrl-cmf!ukma!rutgers!uwvax!oddjob!gargoyle!att!ihnp4!ihlpe!kah120 From: kah120@ihlpe.ATT.COM (Ken Heitke) Newsgroups: comp.sys.mac.programmer Subject: Re: LSC malloc returning garbage? Message-ID: <3472@ihlpe.ATT.COM> Date: 12 Sep 88 15:33:52 GMT References: <1451@murdu.OZ> Organization: AT&T Bell Laboratories - Naperville, Illinois Lines: 29 In article <1451@murdu.OZ>, strasser@murdu.OZ (Mike Strasser) writes: > A month or so ago I posted a query about a program I'd ported to LSC > which had run extensively under VAX/VMS, Unix and MS-DOS, and which was > hanging. Today I've had more time to check it out, and it seems that > malloc() is returning nonsense. > > It is called and the result cast to a pointer to a structure I use: > > Sp = (Species *) malloc( sizeof( Species ) ); > > I check for a NULL return, which does not happen. Instead, Sp is loaded > the value 0x00000003 every time on repeated calls! I'm assuming that the > statement: > > printf( "Sp is %08X\n", (unsigned long) Sp ); I don't know if this is the same problem I struggled with over the weekend but have you included Storage.h or declared malloc to be a function which returns a pointer (i.e. void *malloc()). This declaration exists in Storage.h and without it the compiler by defaults thinks malloc is returning an int and you basically only get 16 bits of your pointer (since pointers and ints are different sizes). I really got nailed good by this because for most UNIX programs one never declares malloc because you have to cast it anyways. LSC requires that special include file which you doesn't exist in UNIX land. BTW I find using printf ("Ptr = %lX\n", Sp); to work. I think in your print statement the printf assumes it is reading an int and just pads it with zeros to get 8 digits. Ken Heitke att!ihlpe!kah120 is