[net.micro.amiga] Pointers and AllocMem with Aztec

lbg@gitpyr.UUCP (Lee B Grey) (06/11/86)

I have spent all day trying to get AllocMem to work with my Aztec
compiler.  I refuse to break down and use 32-bit integers.  So
what am I supposed to do??  I have tried the code in the RKM,
which does not work.  I have tried declaring AllocMem() as:
  extern APTR         long             APTR
  extern APTR *       long *           APTR *
  struct APTR *       char             void
  struct APTR         char *           void *
None of them work.  They all return a NULL pointer!  BTW, I am not
as dumb as this looks.  I knew that void would not work.  But take a
look at Aztec's list of Amiga functions AND at the functions.h file
in the include directory -- they both list AllocMem as a void of all
things!!

Bottom line:  If anyone can tell me how to AllocMem a structure (I am
trying for FileInfoBlock), without using the 32-bit compiler option and
32-bit linker library, PLEASE let me know!  Please reply through mail, as
I am too busy banging my head against the wall to read news with any
regularity.

Thanks, dudes and dudettes!

Lee

higgin@cbmvax.UUCP (06/12/86)

In article <1868@gitpyr.UUCP> lbg@gitpyr.UUCP (Lee B Grey) writes:
>
>I have spent all day trying to get AllocMem to work with my Aztec
>compiler.  I refuse to break down and use 32-bit integers.  So
>what am I supposed to do??  I have tried the code in the RKM,
>which does not work.  I have tried declaring AllocMem() as:
>  extern APTR         long             APTR
>  extern APTR *       long *           APTR *
>  struct APTR *       char             void
>  struct APTR         char *           void *
>None of them work.  They all return a NULL pointer!  BTW, I am not
>as dumb as this looks.  I knew that void would not work.  But take a
>look at Aztec's list of Amiga functions AND at the functions.h file
>in the include directory -- they both list AllocMem as a void of all
>things!!
>
>Bottom line:  If anyone can tell me how to AllocMem a structure (I am
>trying for FileInfoBlock), without using the 32-bit compiler option and
>32-bit linker library, PLEASE let me know!  Please reply through mail, as
>I am too busy banging my head against the wall to read news with any
>regularity.
>
>Thanks, dudes and dudettes!
>
>Lee

Well Lee, I hope you get this on the net, I sypathize with you - but I
had no trouble, and am one ecstatically happy Aztec C user, and I NEVER
use the 32 bit option.

The trick you're probably missing is not casting sizeof( to a long, because
AllocMem() and ALL NON-AZTEC FUNCTIONS expect LONGS for all numeric
parameters.  The following fragment will work, guaranteed.

#include <libraries/dosextens.h>
#include <exec/memory.h>

extern void *AllocMem();

struct FileInfoBlock *GetAFib()
{
        return(AllocMem((long)sizeof(struct FileInfoBlock), MEMF_PUBLIC));
}

The void * declaration of AllocMem is convenient because Aztec C doesn't
complain about equating pointers to voids to any other type without a
cast.  (This is in the manual).

        Regards,
                Paul Higginbottom

Disclaimer: I do not work for Commodore and my opinions are at least my own.

rico@oscvax.UUCP (06/14/86)

In article <1868@gitpyr.UUCP> lbg@gitpyr.UUCP writes:
>
>I have spent all day trying to get AllocMem to work with my Aztec
>compiler.  I refuse to break down and use 32-bit integers.  So
>what am I supposed to do??  I have tried the code in the RKM,
>which does not work.  I have tried declaring AllocMem() as:
>  extern APTR         long             APTR
>  extern APTR *       long *           APTR *
>  struct APTR *       char             void
>  struct APTR         char *           void *
>
>None of them work.  They all return a NULL pointer!  BTW, I am not
>as dumb as this looks.  I knew that void would not work.  But take a
>look at Aztec's list of Amiga functions AND at the functions.h file
>in the include directory -- they both list AllocMem as a void of all
>things!!
>
>Thanks, dudes and dudettes!
>
>Lee

Even though Lee asked for replies by mail I thought I'd post this as
well just in case anyone else is stuck on this.  Lee's problem is probably
not how he is declaring Allocmem "char *AllocMem()" should be fine, with
Aztec and 16 bit int's you must be careful how you *call* Allocmem or any
other Resident library routine.  The Amiga library routines all expect
to be passed 32 bit objects i.e. "longs"... so don't use a call like this:


  wrong: foo = (struct bar *) Allocmem( sizeof(struct bar) );

correct: foo = (struct bar *) Allocmem( (long)sizeof(struct bar) );


be especially careful if you are using constants, if you want to allocate
10 bytes, don't say

	foo = Allocmem(10);

say

	foo = Allocmem(10L);


Another warning to novice Aztec users, if you compile with +l to get 32 bit
ints, don't forget to link with the 32 bit library... use c32.lib and m32.lib
they are on the second disk...

	That's all folks...
	  -Rico

	...{ihnp4|allegra|decvax|linus|watmath}!utzoo!oscvax!rico

rico@oscvax.UUCP (06/14/86)

Whooops.... Allocmem() takes a second argument doesn't it... something
about what kind of memory you want to allocate (chip, fast, don't care...)

Well I sort of left that out of the examples in my posting, so, like, use
your imagination (should have checked my manuals (which I still haven't done))

	Open mouth, insert foot.
		-Rico