[comp.sys.mac.programmer] Please help me allocate memory

jp48+@andrew.cmu.edu (Jonathan Pace) (05/11/91)

   I'm working with a pointer to several arrays of type long.  The pointer is
used as a global.  I can't figure out when and where to declare the sizes of
the various arrays.  I have a structure as follows

    instance {
      long *x;
      long *y;
      long *id;
    } ;

and I know I want to declare the size of the x, y, and id arrays to be MAX_SIZE
which I declare as a macro.  Can someone please tell me how I should indicate
to my program the size of these arrays.  I'm getting desperate.

Jon

p.s. - please speak in simple terms, I don't have much experience programming
the Mac in Think C.

sdd@oceania (05/14/91)

In article <sc_syUS00WB9ECpENw@andrew.cmu.edu> jp48+@andrew.cmu.edu (Jonathan  
Pace) writes:
> 
>    I'm working with a pointer to several arrays of type long.  The pointer is
> used as a global.  I can't figure out when and where to declare the sizes of
> the various arrays.  I have a structure as follows
> 
>     instance {
>       long *x;
>       long *y;
>       long *id;
>     } ;
> 
> and I know I want to declare the size of the x, y, and id arrays to be  
MAX_SIZE
> which I declare as a macro.  Can someone please tell me how I should indicate
> to my program the size of these arrays.
> 
> p.s. - please speak in simple terms, I don't have much experience programming
> the Mac in Think C.


typedef instance	/* se we can use instance as a variable type	*/
{
	long	*x, *y, *id;
} instance;


SomeFn()
{
	instance	try;

/* don't do both of the following NewPtr allocations			*/
/* if MAX_SIZE is the total space needed for all elements, then:	*/
	try.x = (long *)NewPtr(MAX_SIZE);
	try.y = (long *)NewPtr(MAX_SIZE);
	try.id = (long *)NewPtr(MAX_SIZE);
/* if MAX_SIZE is the number of elements you want in each array:	*/
	try.x = (long *)NewPtr(MAX_SIZE * sizeof(long));
	try.y = (long *)NewPtr(MAX_SIZE * sizeof(long));
	try.id = (long *)NewPtr(MAX_SIZE * sizeof(long));

	/* do junk with try	*/
	/* ex: set the fourth element in try.y to the first element	*/
	/*	in try.x (make sure you've allocated enough room before */
	/*	doing this).						*/
	try.y[3] = try.x[0];

	/* when you're done:	*/
	DisposPtr(try.x);
	DisposPtr(try.y);
	DisposPtr(try.id);
}

the code presented here has not been compiled nor proven to be correct.  I  
disclaim any warranty of any kind, expressed or implied, as to its
fitness for any particular use.  Just wanted to keep all the syntactical error  
responses off my back.

Steve

--
+-----------------------------------+
|            Steve Dakin            |
|     oceania!sdd@uunet.uu.net      |
|            (NeXT mail)            |
| tread lighty so others may follow |
+-----------------------------------+

-- 
+-----------------------------------+
|            Steve Dakin            |
|     oceania!sdd@uunet.uu.net      |
|            (NeXT mail)            |