[comp.sys.mac.programmer] I can't malloc

dmr@csli.Stanford.EDU (Daniel M. Rosenberg) (08/21/90)

It used to be pretty easy for me to program in C on some UNIX box,
but at the moment I'm writing a C application making heavy use of inline
assembly code (in Think C 4.0) on a Macintosh.

calloc and malloc don't seem to return anything, so I figured I did it wrong.
I then said to me, hey, why not be Mac-like, and try NewHandle? Which
I did, and got a pointer to a pointer to some area of memory, which I promptly
filled up (I asked for 5000 * 2 bytes and filled it with 5000 words)
and which promptly crashed the machine.

I filled the memory with assembly code, loading an address register with
the memory location.

Anyhow, I'm making some obvious mistake, but my frustration level during the
past week has been such that I have gotten nowhere.

Does anyone have a short code segment showing some simple memory allocation
off the heap, and how to access it? I'd be forever grateful.

Thanks,
Dan

-- 
# Daniel M. Rosenberg    //  Stanford CSLI  // Chew my opinions, not Stanford's.
# dmr@csli.stanford.edu // decwrl!csli!dmr // dmr%csli@stanford.bitnet

russotto@eng.umd.edu (Matthew T. Russotto) (08/22/90)

In article <14983@csli.Stanford.EDU> dmr@csli.Stanford.EDU (Daniel M. Rosenberg) writes:
>calloc and malloc don't seem to return anything, so I figured I did it wrong.
>I then said to me, hey, why not be Mac-like, and try NewHandle? Which
>I did, and got a pointer to a pointer to some area of memory, which I promptly
>filled up (I asked for 5000 * 2 bytes and filled it with 5000 words)
>and which promptly crashed the machine.

If someone is collecting a list of frequently asked questions, this one is at
the top.

To use malloc and calloc in Think C, you must #include <stdlib.h>.  To use
the Mac memory routines, #include <MacTraps>.  

The problem you are running into is that malloc, calloc, NewHandle, and NewPtr
all expect a long and return a pointer, and that by default, Think C will
pass an int (calloc and malloc only) and return an int (all of them).
--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
][, ][+, ///, ///+, //e, //c, IIGS, //c+ --- Any questions?