Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uflorida!kluge!serss0!corrie From: corrie@serss0.fiu.edu (Corrie van der Merwe) Newsgroups: comp.sys.mac.programmer Subject: Re: TC 4.0 and linked list -- Thanks to everyone who answered... Message-ID: <1039@kluge.fiu.edu> Date: 28 Jul 90 01:11:33 GMT Sender: news@kluge.fiu.edu Distribution: na Organization: Florida International University, Miami Lines: 74 Thanks to everyone who answered my question concerning dynamic allocation using malloc/calloc on a Mac+. For those people who didn't quite give the right advice, I thought I would post the results to share with anyone caring to glance across this message. The problem was that I kept getting a 'out of memory' error whenever I tried to allocate more than 3 nodes (at least I thought I was asking for only 3 nodes :-) ). As someone pointed out, #include the correct c library so that the prototype will be correct. An very bad oversight on my part. The correct library in THINK C 4.0 when using malloc() is . The other major oversight, (and the main cause of my problem) was that THINK decided (in their infinite wisdom :-) ) to implement int.s as 16 bit quantities instead of 32 bit quantities. In addition to this, THINK decided that the sizeof() function should return an int instead of size_t like it should. size_t, by the way, is an unsigned long int. These factors caused something like this: (some of you may laugh at this point ;-} ) int *head; ... if( (head=(Node *)malloc(sizeof(Node))) == NULL ) { printf("\nout of memory again\n"); exit(0); } malloc() expects to see something which is size_t, but sizeof() was only returning a 16-bit int. malloc probably grabbed 32 bits anyway, and voila-- I requested whatever garbage was in the 32 bits from malloc(). I changed the above code to: head=(Node *)malloc((long)sizeof(Node)) and everyting worked fine. I tried to get 1000 nodes -- which I did without a problem. There might be a better way: (as someone suggested) head=(Node *)malloc((size_t)sizeof(Node)) Again many thanks for everyone who responded so quickly. (of course, THINK does doccument that sizeof() returns an int instead of size_t, but I did not read that part. I did know that an int in THINK C was 16 bits though.) One person suggested I use Handles instead of using malloc/calloc and free(). The reason for this is (he is very correct by the way -- Most of the mac programming books say this as well) that the Memory Manager will only properly relocate Handles unless you use Hlock to lock the memory. I will be sure to use Handles, and other built in Mac system calls when I write software which will run only on the mac, but for the moment, I am trying to write 'portable' code. Since most of the stuff I am doing is just data structures type program/simulations, I should be able to write the portable code. Keep Handles in mind though if you are going to write 'real' Mac software. Again many thanks. (I guess paying carefull attention to the documentation is actually important :-) ) Corrie PS ~ I really hope THINK C 5.0 is a 'GOOD' implementation of C++, which is more portable than their object extension in TC 4.0