Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!wuarchive!texbell!bellcore!flash!sdh From: sdh@flash.bellcore.com (Stephen D Hawley) Newsgroups: comp.sys.mac.programmer Subject: Re: malloc() and calloc() Message-ID: <21250@bellcore.bellcore.com> Date: 23 Mar 90 19:00:50 GMT References: <3860@sage.cc.purdue.edu> Sender: news@bellcore.bellcore.com Reply-To: sdh@flash.UUCP (Stephen D Hawley) Organization: Bellcore, Morristown, NJ Lines: 40 In article <3860@sage.cc.purdue.edu> ar4@sage.cc.purdue.edu (Piper Keairnes) writes: > Well the last time this discussion was around, I didn't know that the >heck these two functions meant (or how C worked for that matter). Things have >changed, but I believe I'm running into the same problem that others did: > > I CAN do the following successfully: > > int *i; > > i = (int*) malloc(sizeof(int) * 5); > > but if CANNOT do the following: > > int *i; > > i = (int*) malloc(sizeof(int) * 6); > > Notice the difference??? Trying to allocate 5 gives me a valid pointer. >Trying to allocate 6 gives me 0x000000. the big NULL! Anyways, anyone know >why this is happening? I'm on a 2.5meg SE (don't believe a lack of memory >to be the problem). Handy hints: Use NewPtr() to get memory if you're doing Mac-only stuff. Think C will promote shorts to longs on tollbox calls, so you don't have to worry about the argument. malloc() takes as its argument type size_t which is an unsigned long. sizeof() evaluates to a short, thus unless you #include stdlib.h, you will be sending malloc the wrong size. Try instead i = (int *)malloc(sizeof(int) * 6L); or i = (int *)malloc((long)(sizeof(int) * 6)); Steve Hawley sdh@flash.bellcore.com "I'm sick and tired of being told that ordinary decent people are fed up with being sick and tired. I know I'm certainly not, and I'm sick and tired of being told that I am."