[mod.sources] dynamic allocation

sources-request@genrad.UUCP (08/15/85)

Mod.sources:  Volume 2, Issue 36
Submitted by: decvax!mcnc!idis!george (George Rosenberg)

I added a call to free in arrayalloc so that memory would not be lost.

I also wrote a header file, "alloc2.h", with some macros to make a better
interface to the package.  Usage should be obvious.  Note that in this
implementation of the macro FREE2D, three of the four arguments are ignored.
These were included both for uniformity and to allow for other implementations.
Other implementations may also want to provide for dimensions of type long.
Use of the macros would simplify this.

	George Rosenberg

	duke!mcnc!idis!george
	decvax!idis!george



*** new:arrayalloc.c	Wed Aug 14 10:02:48 1985
--- old:arrayalloc.c	Wed Aug 14 09:32:53 1985
***************
*** 49,56
  	/*
  	 * Get memory for intermediate row address vector.
  	 */
! 	if ((vector = (char **) malloc (imax * sizeof (char *))) == NULL) {
! 		free(array) ;
  		return (NULL);
- 	}
  

--- 49,55 -----
  	/*
  	 * Get memory for intermediate row address vector.
  	 */
! 	if ((vector = (char **) malloc (imax * sizeof (char *))) == NULL)
  		return (NULL);
  
  	/*




----
Below is the contents of file "alloc2.h".
----
extern char **arrayalloc() ;
extern void arrayfree() ;

#define	ALLOC2D(type,dim0,dim1) \
	(type**)arrayalloc((unsigned)dim0,(unsigned)dim1,(unsigned)sizeof(type))
#define	FREE2D(type,addr,dim0,dim1) arrayfree((char**)addr)
----